descr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* Copyright (C) 2002-2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _DESCR_H
  16. #define _DESCR_H 1
  17. #include <limits.h>
  18. #include <sched.h>
  19. #include <setjmp.h>
  20. #include <stdbool.h>
  21. #include <sys/types.h>
  22. #include <hp-timing.h>
  23. #include <list.h>
  24. #include <lowlevellock.h>
  25. #include <pthreaddef.h>
  26. #include "../nptl_db/thread_db.h"
  27. #include <tls.h>
  28. #ifdef HAVE_FORCED_UNWIND
  29. # include <unwind.h>
  30. #endif
  31. #define __need_res_state
  32. #include <resolv.h>
  33. #include <bits/kernel-features.h>
  34. #include "uClibc-glue.h"
  35. #ifndef TCB_ALIGNMENT
  36. # define TCB_ALIGNMENT sizeof (double)
  37. #endif
  38. /* We keep thread specific data in a special data structure, a two-level
  39. array. The top-level array contains pointers to dynamically allocated
  40. arrays of a certain number of data pointers. So we can implement a
  41. sparse array. Each dynamic second-level array has
  42. PTHREAD_KEY_2NDLEVEL_SIZE
  43. entries. This value shouldn't be too large. */
  44. #define PTHREAD_KEY_2NDLEVEL_SIZE 32
  45. /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
  46. keys in each subarray. */
  47. #define PTHREAD_KEY_1STLEVEL_SIZE \
  48. ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
  49. / PTHREAD_KEY_2NDLEVEL_SIZE)
  50. /* Internal version of the buffer to store cancellation handler
  51. information. */
  52. struct pthread_unwind_buf
  53. {
  54. struct
  55. {
  56. __jmp_buf jmp_buf;
  57. int mask_was_saved;
  58. } cancel_jmp_buf[1];
  59. union
  60. {
  61. /* This is the placeholder of the public version. */
  62. void *pad[4];
  63. struct
  64. {
  65. /* Pointer to the previous cleanup buffer. */
  66. struct pthread_unwind_buf *prev;
  67. /* Backward compatibility: state of the old-style cleanup
  68. handler at the time of the previous new-style cleanup handler
  69. installment. */
  70. struct _pthread_cleanup_buffer *cleanup;
  71. /* Cancellation type before the push call. */
  72. int canceltype;
  73. } data;
  74. } priv;
  75. };
  76. /* Opcodes and data types for communication with the signal handler to
  77. change user/group IDs. */
  78. struct xid_command
  79. {
  80. int syscall_no;
  81. long int id[3];
  82. volatile int cntr;
  83. };
  84. /* Data structure used by the kernel to find robust futexes. */
  85. struct robust_list_head
  86. {
  87. void *list;
  88. long int futex_offset;
  89. void *list_op_pending;
  90. };
  91. /* Data strcture used to handle thread priority protection. */
  92. struct priority_protection_data
  93. {
  94. int priomax;
  95. unsigned int priomap[];
  96. };
  97. /* Thread descriptor data structure. */
  98. struct pthread
  99. {
  100. union
  101. {
  102. #if !defined(TLS_DTV_AT_TP)
  103. /* This overlaps the TCB as used for TLS without threads (see tls.h). */
  104. tcbhead_t header;
  105. #else
  106. struct
  107. {
  108. int multiple_threads;
  109. int gscope_flag;
  110. # ifndef __ASSUME_PRIVATE_FUTEX
  111. int private_futex;
  112. # endif
  113. } header;
  114. #endif
  115. /* This extra padding has no special purpose, and this structure layout
  116. is private and subject to change without affecting the official ABI.
  117. We just have it here in case it might be convenient for some
  118. implementation-specific instrumentation hack or suchlike. */
  119. void *__padding[24];
  120. };
  121. /* This descriptor's link on the `stack_used' or `__stack_user' list. */
  122. list_t list;
  123. /* Thread ID - which is also a 'is this thread descriptor (and
  124. therefore stack) used' flag. */
  125. pid_t tid;
  126. /* Process ID - thread group ID in kernel speak. */
  127. pid_t pid;
  128. /* List of robust mutexes the thread is holding. */
  129. #ifdef __PTHREAD_MUTEX_HAVE_PREV
  130. void *robust_prev;
  131. struct robust_list_head robust_head;
  132. /* The list above is strange. It is basically a double linked list
  133. but the pointer to the next/previous element of the list points
  134. in the middle of the object, the __next element. Whenever
  135. casting to __pthread_list_t we need to adjust the pointer
  136. first. */
  137. # define QUEUE_PTR_ADJUST (offsetof (__pthread_list_t, __next))
  138. # define ENQUEUE_MUTEX_BOTH(mutex, val) \
  139. do { \
  140. __pthread_list_t *next = (__pthread_list_t *) \
  141. ((((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_head.list)) & ~1ul) \
  142. - QUEUE_PTR_ADJUST); \
  143. next->__prev = (void *) &mutex->__data.__list.__next; \
  144. mutex->__data.__list.__next = THREAD_GETMEM (THREAD_SELF, \
  145. robust_head.list); \
  146. mutex->__data.__list.__prev = (void *) &THREAD_SELF->robust_head; \
  147. THREAD_SETMEM (THREAD_SELF, robust_head.list, \
  148. (void *) (((uintptr_t) &mutex->__data.__list.__next) \
  149. | val)); \
  150. } while (0)
  151. # define DEQUEUE_MUTEX(mutex) \
  152. do { \
  153. __pthread_list_t *next = (__pthread_list_t *) \
  154. ((char *) (((uintptr_t) mutex->__data.__list.__next) & ~1ul) \
  155. - QUEUE_PTR_ADJUST); \
  156. next->__prev = mutex->__data.__list.__prev; \
  157. __pthread_list_t *prev = (__pthread_list_t *) \
  158. ((char *) (((uintptr_t) mutex->__data.__list.__prev) & ~1ul) \
  159. - QUEUE_PTR_ADJUST); \
  160. prev->__next = mutex->__data.__list.__next; \
  161. mutex->__data.__list.__prev = NULL; \
  162. mutex->__data.__list.__next = NULL; \
  163. } while (0)
  164. #else
  165. union
  166. {
  167. __pthread_slist_t robust_list;
  168. struct robust_list_head robust_head;
  169. };
  170. # define ENQUEUE_MUTEX_BOTH(mutex, val) \
  171. do { \
  172. mutex->__data.__list.__next \
  173. = THREAD_GETMEM (THREAD_SELF, robust_list.__next); \
  174. THREAD_SETMEM (THREAD_SELF, robust_list.__next, \
  175. (void *) (((uintptr_t) &mutex->__data.__list) | val)); \
  176. } while (0)
  177. # define DEQUEUE_MUTEX(mutex) \
  178. do { \
  179. __pthread_slist_t *runp = (__pthread_slist_t *) \
  180. (((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_list.__next)) & ~1ul); \
  181. if (runp == &mutex->__data.__list) \
  182. THREAD_SETMEM (THREAD_SELF, robust_list.__next, runp->__next); \
  183. else \
  184. { \
  185. __pthread_slist_t *next = (__pthread_slist_t *) \
  186. (((uintptr_t) runp->__next) & ~1ul); \
  187. while (next != &mutex->__data.__list) \
  188. { \
  189. runp = next; \
  190. next = (__pthread_slist_t *) (((uintptr_t) runp->__next) & ~1ul); \
  191. } \
  192. \
  193. runp->__next = next->__next; \
  194. mutex->__data.__list.__next = NULL; \
  195. } \
  196. } while (0)
  197. #endif
  198. #define ENQUEUE_MUTEX(mutex) ENQUEUE_MUTEX_BOTH (mutex, 0)
  199. #define ENQUEUE_MUTEX_PI(mutex) ENQUEUE_MUTEX_BOTH (mutex, 1)
  200. /* List of cleanup buffers. */
  201. struct _pthread_cleanup_buffer *cleanup;
  202. /* Unwind information. */
  203. struct pthread_unwind_buf *cleanup_jmp_buf;
  204. #define HAVE_CLEANUP_JMP_BUF
  205. /* Flags determining processing of cancellation. */
  206. int cancelhandling;
  207. /* Bit set if cancellation is disabled. */
  208. #define CANCELSTATE_BIT 0
  209. #define CANCELSTATE_BITMASK (0x01 << CANCELSTATE_BIT)
  210. /* Bit set if asynchronous cancellation mode is selected. */
  211. #define CANCELTYPE_BIT 1
  212. #define CANCELTYPE_BITMASK (0x01 << CANCELTYPE_BIT)
  213. /* Bit set if canceling has been initiated. */
  214. #define CANCELING_BIT 2
  215. #define CANCELING_BITMASK (0x01 << CANCELING_BIT)
  216. /* Bit set if canceled. */
  217. #define CANCELED_BIT 3
  218. #define CANCELED_BITMASK (0x01 << CANCELED_BIT)
  219. /* Bit set if thread is exiting. */
  220. #define EXITING_BIT 4
  221. #define EXITING_BITMASK (0x01 << EXITING_BIT)
  222. /* Bit set if thread terminated and TCB is freed. */
  223. #define TERMINATED_BIT 5
  224. #define TERMINATED_BITMASK (0x01 << TERMINATED_BIT)
  225. /* Bit set if thread is supposed to change XID. */
  226. #define SETXID_BIT 6
  227. #define SETXID_BITMASK (0x01 << SETXID_BIT)
  228. /* Mask for the rest. Helps the compiler to optimize. */
  229. #define CANCEL_RESTMASK 0xffffff80
  230. #define CANCEL_ENABLED_AND_CANCELED(value) \
  231. (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \
  232. | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
  233. #define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
  234. (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK \
  235. | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK)) \
  236. == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
  237. /* Flags. Including those copied from the thread attribute. */
  238. int flags;
  239. /* We allocate one block of references here. This should be enough
  240. to avoid allocating any memory dynamically for most applications. */
  241. struct pthread_key_data
  242. {
  243. /* Sequence number. We use uintptr_t to not require padding on
  244. 32- and 64-bit machines. On 64-bit machines it helps to avoid
  245. wrapping, too. */
  246. uintptr_t seq;
  247. /* Data pointer. */
  248. void *data;
  249. } specific_1stblock[PTHREAD_KEY_2NDLEVEL_SIZE];
  250. /* Two-level array for the thread-specific data. */
  251. struct pthread_key_data *specific[PTHREAD_KEY_1STLEVEL_SIZE];
  252. /* Flag which is set when specific data is set. */
  253. bool specific_used;
  254. /* True if events must be reported. */
  255. bool report_events;
  256. /* True if the user provided the stack. */
  257. bool user_stack;
  258. /* True if thread must stop at startup time. */
  259. bool stopped_start;
  260. /* The parent's cancel handling at the time of the pthread_create
  261. call. This might be needed to undo the effects of a cancellation. */
  262. int parent_cancelhandling;
  263. /* Lock to synchronize access to the descriptor. */
  264. int lock;
  265. /* Lock for synchronizing setxid calls. */
  266. int setxid_futex;
  267. #if HP_TIMING_AVAIL
  268. /* Offset of the CPU clock at start thread start time. */
  269. hp_timing_t cpuclock_offset;
  270. #endif
  271. /* If the thread waits to join another one the ID of the latter is
  272. stored here.
  273. In case a thread is detached this field contains a pointer of the
  274. TCB if the thread itself. This is something which cannot happen
  275. in normal operation. */
  276. struct pthread *joinid;
  277. /* Check whether a thread is detached. */
  278. #define IS_DETACHED(pd) ((pd)->joinid == (pd))
  279. /* The result of the thread function. */
  280. void *result;
  281. /* Scheduling parameters for the new thread. */
  282. struct sched_param schedparam;
  283. int schedpolicy;
  284. /* Start position of the code to be executed and the argument passed
  285. to the function. */
  286. void *(*start_routine) (void *);
  287. void *arg;
  288. /* Debug state. */
  289. td_eventbuf_t eventbuf;
  290. /* Next descriptor with a pending event. */
  291. struct pthread *nextevent;
  292. #ifdef HAVE_FORCED_UNWIND
  293. /* Machine-specific unwind info. */
  294. struct _Unwind_Exception exc;
  295. #endif
  296. /* If nonzero pointer to area allocated for the stack and its
  297. size. */
  298. void *stackblock;
  299. size_t stackblock_size;
  300. /* Size of the included guard area. */
  301. size_t guardsize;
  302. /* This is what the user specified and what we will report. */
  303. size_t reported_guardsize;
  304. /* Thread Priority Protection data. */
  305. struct priority_protection_data *tpp;
  306. /* Resolver state. */
  307. struct __res_state res;
  308. /* This member must be last. */
  309. char end_padding[];
  310. #define PTHREAD_STRUCT_END_PADDING \
  311. (sizeof (struct pthread) - offsetof (struct pthread, end_padding))
  312. } __attribute ((aligned (TCB_ALIGNMENT)));
  313. #endif /* descr.h */