emutls.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* TLS emulation.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "tconfig.h"
  21. #include "tsystem.h"
  22. #include "coretypes.h"
  23. #include "tm.h"
  24. #include "libgcc_tm.h"
  25. #include "gthr.h"
  26. typedef unsigned int word __attribute__((mode(word)));
  27. typedef unsigned int pointer __attribute__((mode(pointer)));
  28. struct __emutls_object
  29. {
  30. word size;
  31. word align;
  32. union {
  33. pointer offset;
  34. void *ptr;
  35. } loc;
  36. void *templ;
  37. };
  38. struct __emutls_array
  39. {
  40. pointer size;
  41. void **data[];
  42. };
  43. /* EMUTLS_ATTR is provided to allow targets to build the emulated tls
  44. routines as weak definitions, for example.
  45. If there is no definition, fall back to the default. */
  46. #ifndef EMUTLS_ATTR
  47. # define EMUTLS_ATTR
  48. #endif
  49. EMUTLS_ATTR
  50. void *__emutls_get_address (struct __emutls_object *);
  51. EMUTLS_ATTR
  52. void __emutls_register_common (struct __emutls_object *, word, word, void *);
  53. #ifdef __GTHREADS
  54. #ifdef __GTHREAD_MUTEX_INIT
  55. static __gthread_mutex_t emutls_mutex = __GTHREAD_MUTEX_INIT;
  56. #else
  57. static __gthread_mutex_t emutls_mutex;
  58. #endif
  59. static __gthread_key_t emutls_key;
  60. static pointer emutls_size;
  61. static void
  62. emutls_destroy (void *ptr)
  63. {
  64. struct __emutls_array *arr = ptr;
  65. pointer size = arr->size;
  66. pointer i;
  67. for (i = 0; i < size; ++i)
  68. {
  69. if (arr->data[i])
  70. free (arr->data[i][-1]);
  71. }
  72. free (ptr);
  73. }
  74. static void
  75. emutls_init (void)
  76. {
  77. #ifndef __GTHREAD_MUTEX_INIT
  78. __GTHREAD_MUTEX_INIT_FUNCTION (&emutls_mutex);
  79. #endif
  80. if (__gthread_key_create (&emutls_key, emutls_destroy) != 0)
  81. abort ();
  82. }
  83. #endif
  84. static void *
  85. emutls_alloc (struct __emutls_object *obj)
  86. {
  87. void *ptr;
  88. void *ret;
  89. /* We could use here posix_memalign if available and adjust
  90. emutls_destroy accordingly. */
  91. if (obj->align <= sizeof (void *))
  92. {
  93. ptr = malloc (obj->size + sizeof (void *));
  94. if (ptr == NULL)
  95. abort ();
  96. ((void **) ptr)[0] = ptr;
  97. ret = ptr + sizeof (void *);
  98. }
  99. else
  100. {
  101. ptr = malloc (obj->size + sizeof (void *) + obj->align - 1);
  102. if (ptr == NULL)
  103. abort ();
  104. ret = (void *) (((pointer) (ptr + sizeof (void *) + obj->align - 1))
  105. & ~(pointer)(obj->align - 1));
  106. ((void **) ret)[-1] = ptr;
  107. }
  108. if (obj->templ)
  109. memcpy (ret, obj->templ, obj->size);
  110. else
  111. memset (ret, 0, obj->size);
  112. return ret;
  113. }
  114. /* Despite applying the attribute to the declaration, in this case the mis-
  115. match between the builtin's declaration [void * (*)(void *)] and the
  116. implementation here, causes the decl. attributes to be discarded. */
  117. EMUTLS_ATTR void *
  118. __emutls_get_address (struct __emutls_object *obj)
  119. {
  120. if (! __gthread_active_p ())
  121. {
  122. if (__builtin_expect (obj->loc.ptr == NULL, 0))
  123. obj->loc.ptr = emutls_alloc (obj);
  124. return obj->loc.ptr;
  125. }
  126. #ifndef __GTHREADS
  127. abort ();
  128. #else
  129. pointer offset = __atomic_load_n (&obj->loc.offset, __ATOMIC_ACQUIRE);
  130. if (__builtin_expect (offset == 0, 0))
  131. {
  132. static __gthread_once_t once = __GTHREAD_ONCE_INIT;
  133. __gthread_once (&once, emutls_init);
  134. __gthread_mutex_lock (&emutls_mutex);
  135. offset = obj->loc.offset;
  136. if (offset == 0)
  137. {
  138. offset = ++emutls_size;
  139. __atomic_store_n (&obj->loc.offset, offset, __ATOMIC_RELEASE);
  140. }
  141. __gthread_mutex_unlock (&emutls_mutex);
  142. }
  143. struct __emutls_array *arr = __gthread_getspecific (emutls_key);
  144. if (__builtin_expect (arr == NULL, 0))
  145. {
  146. pointer size = offset + 32;
  147. arr = calloc (size + 1, sizeof (void *));
  148. if (arr == NULL)
  149. abort ();
  150. arr->size = size;
  151. __gthread_setspecific (emutls_key, (void *) arr);
  152. }
  153. else if (__builtin_expect (offset > arr->size, 0))
  154. {
  155. pointer orig_size = arr->size;
  156. pointer size = orig_size * 2;
  157. if (offset > size)
  158. size = offset + 32;
  159. arr = realloc (arr, (size + 1) * sizeof (void *));
  160. if (arr == NULL)
  161. abort ();
  162. arr->size = size;
  163. memset (arr->data + orig_size, 0,
  164. (size - orig_size) * sizeof (void *));
  165. __gthread_setspecific (emutls_key, (void *) arr);
  166. }
  167. void *ret = arr->data[offset - 1];
  168. if (__builtin_expect (ret == NULL, 0))
  169. {
  170. ret = emutls_alloc (obj);
  171. arr->data[offset - 1] = ret;
  172. }
  173. return ret;
  174. #endif
  175. }
  176. EMUTLS_ATTR void
  177. __emutls_register_common (struct __emutls_object *obj,
  178. word size, word align, void *templ)
  179. {
  180. if (obj->size < size)
  181. {
  182. obj->size = size;
  183. obj->templ = NULL;
  184. }
  185. if (obj->align < align)
  186. obj->align = align;
  187. if (templ && size == obj->size)
  188. obj->templ = templ;
  189. }