Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
7.1 KiB

  1. /* $OpenBSD: tib.h,v 1.7 2019/05/10 13:29:21 guenther Exp $ */
  2. /*
  3. * Copyright (c) 2011,2014 Philip Guenther <guenther@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Thread Information Block (TIB) and Thread Local Storage (TLS) handling
  19. * (the TCB, Thread Control Block, is part of the TIB)
  20. */
  21. #ifndef _TIB_H_
  22. #define _TIB_H_
  23. #include <sys/types.h>
  24. #include <machine/tcb.h>
  25. #include <stddef.h>
  26. /*
  27. * This header defines struct tib and at least eight macros:
  28. * TLS_VARIANT
  29. * Either 1 or 2 (Actually defined by <machine/tcb.h>)
  30. *
  31. * TCB_SET(tcb)
  32. * Set the TCB pointer for this thread to 'tcb'
  33. *
  34. * TCB_GET()
  35. * Return the TCB pointer for this thread
  36. *
  37. * TCB_TO_TIB(tcb)
  38. * Given a TCB pointer, return the matching TIB pointer
  39. *
  40. * TIB_TO_TCB(tib)
  41. * Given a TIB pointer, return the matching TCB pointer
  42. *
  43. * TIB_INIT(tib, dtv, thread)
  44. * Initializes a TIB for a new thread, using the supplied
  45. * values for its dtv and thread pointers
  46. *
  47. * TIB_GET()
  48. * Short-hand for TCB_TO_TIB(TCB_GET())
  49. *
  50. * TIB_EXTRA_ALIGN
  51. * On TLS varaint 2 archs, what alignment is sufficient
  52. * for the extra space that will be used for struct pthread?
  53. *
  54. * The following functions are provided by either ld.so (dynamic) or
  55. * libc (static) for allocating and freeing a common memory block that
  56. * will hold both the TIB and the pthread structure:
  57. * _dl_allocate_tib(sizeof(struct pthread))
  58. * Allocates a combined TIB and pthread memory region.
  59. * The argument is the amount of space to reserve
  60. * for the pthread structure. Returns a pointer to
  61. * the TIB inside the allocated block.
  62. *
  63. * _dl_free_tib(tib, sizeof(struct pthread))
  64. * Frees a TIB and pthread block previously allocated
  65. * with _dl_allocate_tib(). Must be passed the return
  66. * value of that previous call.
  67. */
  68. /*
  69. * Regarding <machine/tcb.h>:
  70. * - it must define the TLS_VARIANT macro
  71. * - it may define TCB_OFFSET if the TCB address in the kernel and/or
  72. * register is offset from the actual TCB address. TCB_OFFSET > 0
  73. * means the kernel/register points to *after* the real data.
  74. * - if there's a faster way to get or set the TCB pointer for the thread
  75. * than the __{get,set}_tcb() syscalls, it should define either or both
  76. * the TCB_{GET,SET} macros to do so.
  77. */
  78. /* All archs but mips64 have fast TCB_GET() and don't need caching */
  79. #ifndef __mips64__
  80. # define TCB_HAVE_MD_GET 1
  81. #endif
  82. #ifdef TCB_SET
  83. # define TCB_HAVE_MD_SET 1
  84. #else
  85. # define TCB_SET(tcb) __set_tcb(tcb)
  86. #endif
  87. #ifndef TCB_OFFSET
  88. # define TCB_OFFSET 0
  89. #endif
  90. /*
  91. * tib_cantcancel values is non-zero if the thread should skip all
  92. * cancellation processing
  93. */
  94. #define CANCEL_DISABLED 1
  95. #define CANCEL_DYING 2
  96. /*
  97. * tib_cancel_point is non-zero if we're in a cancel point; its modified
  98. * by the cancel point code and read by the cancellation signal handler
  99. */
  100. #define CANCEL_POINT 1
  101. #define CANCEL_POINT_DELAYED 2
  102. #if TLS_VARIANT == 1
  103. /*
  104. * ABI specifies that the static TLS data starts two words after the
  105. * (notional) thread pointer, with the first of those two words being
  106. * the TLS dtv pointer. The other (second) word is reserved for the
  107. * implementation, so we place the pointer to the thread structure there,
  108. * but we place our actual thread bits before the TCB, at negative offsets
  109. * from the TCB pointer. Ergo, memory is laid out, low to high, as:
  110. *
  111. * [pthread structure]
  112. * TIB {
  113. * ...cancelation and other int-sized info...
  114. * int errno
  115. * void *locale
  116. * TCB (- TCB_OFFSET) {
  117. * void *dtv
  118. * struct pthread *thread
  119. * }
  120. * }
  121. * static TLS data
  122. */
  123. struct tib {
  124. void *tib_atexit;
  125. int tib_thread_flags; /* internal to libpthread */
  126. pid_t tib_tid;
  127. int tib_cantcancel;
  128. int tib_cancel_point;
  129. int tib_canceled;
  130. int tib_errno;
  131. void *tib_locale;
  132. void *tib_dtv; /* internal to the runtime linker */
  133. void *tib_thread;
  134. };
  135. #elif TLS_VARIANT == 2
  136. /*
  137. * ABI specifies that the static TLS data occupies the memory before
  138. * the TCB pointer, at negative offsets, and that on i386 and amd64
  139. * the word the TCB points to contains a pointer to itself. So,
  140. * we place errno and our thread bits after that. Memory is laid
  141. * out, low to high, as:
  142. * static TLS data
  143. * TIB {
  144. * TCB (- TCB_OFFSET) {
  145. * self pointer [i386/amd64 only]
  146. * void *dtv
  147. * }
  148. * struct pthread *thread
  149. * void *locale
  150. * int errno
  151. * ...cancelation and other int-sized info...
  152. * }
  153. * [pthread structure]
  154. */
  155. struct tib {
  156. #if defined(__i386) || defined(__amd64)
  157. struct tib *__tib_self;
  158. # define __tib_tcb __tib_self
  159. #endif
  160. void *tib_dtv; /* internal to the runtime linker */
  161. void *tib_thread;
  162. void *tib_locale;
  163. int tib_errno;
  164. int tib_canceled;
  165. int tib_cancel_point;
  166. int tib_cantcancel;
  167. pid_t tib_tid;
  168. int tib_thread_flags; /* internal to libpthread */
  169. void *tib_atexit;
  170. };
  171. #if defined(__i386) || defined(__amd64)
  172. # define _TIB_PREP(tib) \
  173. ((void)((tib)->__tib_self = (tib)))
  174. #endif
  175. #define TIB_EXTRA_ALIGN sizeof(void *)
  176. #else
  177. # error "unknown TLS variant"
  178. #endif
  179. /* nothing to do by default */
  180. #ifndef _TIB_PREP
  181. # define _TIB_PREP(tib) ((void)0)
  182. #endif
  183. #define TIB_INIT(tib, dtv, thread) do { \
  184. (tib)->tib_thread = (thread); \
  185. (tib)->tib_atexit = NULL; \
  186. (tib)->tib_locale = NULL; \
  187. (tib)->tib_cantcancel = 0; \
  188. (tib)->tib_cancel_point = 0; \
  189. (tib)->tib_canceled = 0; \
  190. (tib)->tib_dtv = (dtv); \
  191. (tib)->tib_errno = 0; \
  192. _TIB_PREP(tib); \
  193. } while (0)
  194. #ifndef __tib_tcb
  195. # define __tib_tcb tib_dtv
  196. #endif
  197. #define _TIBO_TCB (offsetof(struct tib, __tib_tcb) + TCB_OFFSET)
  198. #define TCB_TO_TIB(tcb) ((struct tib *)((char *)(tcb) - _TIBO_TCB))
  199. #define TIB_TO_TCB(tib) ((char *)(tib) + _TIBO_TCB)
  200. #define TIB_GET() TCB_TO_TIB(TCB_GET())
  201. __BEGIN_DECLS
  202. struct dl_info;
  203. struct dl_phdr_info;
  204. struct dl_cb_0 {
  205. void *(*dl_allocate_tib)(size_t);
  206. void (*dl_free_tib)(void *, size_t);
  207. void (*dl_clean_boot)(void);
  208. void *(*dlopen)(const char *, int);
  209. int (*dlclose)(void *);
  210. void *(*dlsym)(void *, const char *);
  211. int (*dladdr)(const void *, struct dl_info *);
  212. int (*dlctl)(void *, int, void *);
  213. char *(*dlerror)(void);
  214. int (*dl_iterate_phdr)(int (*)(struct dl_phdr_info *,
  215. size_t, void *), void *);
  216. };
  217. #define DL_CB_CUR 0
  218. typedef struct dl_cb_0 dl_cb;
  219. /* type of function passed to init functions that returns a dl_cb */
  220. typedef const void *dl_cb_cb(int _version);
  221. void *_dl_allocate_tib(size_t _extra) __dso_public;
  222. void _dl_free_tib(void *_tib, size_t _extra) __dso_public;
  223. /* The actual syscalls */
  224. void *__get_tcb(void);
  225. void __set_tcb(void *_tcb);
  226. __END_DECLS
  227. #endif /* _TIB_H_ */