2 * mono-tls.c: Low-level TLS support
4 * Thread local variables that are accessed both from native and managed code
5 * are defined here and should be accessed only through this APIs
7 * Copyright 2013 Xamarin, Inc (http://www.xamarin.com)
10 #include <mono/utils/mach-support.h>
15 * On all platforms we should be able to use either __thread or pthread/TlsGetValue.
16 * Certain platforms will support fast tls only when using one of the thread local
17 * storage backends. By default this is __thread if we have HAVE_KW_THREAD defined.
19 * By default all platforms will call into these native getters whenever they need
20 * to get a tls value. On certain platforms we can try to be faster than this and
21 * avoid the call. We call this fast tls and each platform defines its own way to
22 * achieve this. For this, a platform has to define MONO_ARCH_HAVE_INLINED_TLS,
23 * and provide alternative getters/setters for a MonoTlsKey. In order to have fast
24 * getter/setters, the platform has to declare a way to fetch an internal offset
25 * (MONO_THREAD_VAR_OFFSET) which is stored here, and in the arch specific file
26 * probe the system to see if we can use the offset initialized here. If these
27 * run-time checks don't succeed we just use the fallbacks.
29 * In case we would wish to provide fast inlined tls for aot code, we would need
30 * to be sure that, at run-time, these two platform checks would never fail
31 * otherwise the tls getter/setters that we emitted would not work. Normally,
32 * there is little incentive to support this since tls access is most common in
33 * wrappers and managed allocators, both of which are not aot-ed by default.
34 * So far, we never supported inlined fast tls on full-aot systems.
40 #if HAVE_TLS_MODEL_ATTR
42 #if defined(__PIC__) && !defined(PIC)
44 * Must be compiling -fPIE, for executables. Build PIC
45 * but with initial-exec.
46 * http://bugs.gentoo.org/show_bug.cgi?id=165547
49 #define PIC_INITIAL_EXEC
53 * Define this if you want a faster libmono, which cannot be loaded dynamically as a
56 //#define PIC_INITIAL_EXEC
60 #ifdef PIC_INITIAL_EXEC
61 #define MONO_TLS_FAST __attribute__ ((__tls_model__("initial-exec")))
63 #if defined (__powerpc__)
64 /* local dynamic requires a call to __tls_get_addr to look up the
65 TLS block address via the Dynamic Thread Vector. In this case Thread
66 Pointer relative offsets can't be used as this modules TLS was
67 allocated separately (none contiguoiusly) from the initial TLS
70 For now we will disable this. */
73 #define MONO_TLS_FAST __attribute__ ((__tls_model__("local-dynamic")))
79 #define MONO_TLS_FAST __attribute__ ((__tls_model__("local-exec")))
87 /* Runtime offset detection */
88 #if defined(TARGET_AMD64) && !defined(TARGET_MACH) && !defined(HOST_WIN32) /* __thread likely not tested on mac/win */
91 // This only works if libmono is linked into the application
92 #define MONO_THREAD_VAR_OFFSET(var,offset) do { guint64 foo; __asm ("movq " #var "@GOTTPOFF(%%rip), %0" : "=r" (foo)); offset = foo; } while (0)
94 #define MONO_THREAD_VAR_OFFSET(var,offset) do { guint64 foo; __asm ("movq $" #var "@TPOFF, %0" : "=r" (foo)); offset = foo; } while (0)
97 #elif defined(TARGET_X86) && !defined(TARGET_MACH) && !defined(HOST_WIN32) && defined(__GNUC__)
100 #define MONO_THREAD_VAR_OFFSET(var,offset) do { int tmp; __asm ("call 1f; 1: popl %0; addl $_GLOBAL_OFFSET_TABLE_+[.-1b], %0; movl " #var "@gotntpoff(%0), %1" : "=r" (tmp), "=r" (offset)); } while (0)
102 #define MONO_THREAD_VAR_OFFSET(var,offset) __asm ("movl $" #var "@ntpoff, %0" : "=r" (offset))
105 #elif defined(TARGET_ARM64) && !defined(PIC)
107 #define MONO_THREAD_VAR_OFFSET(var,offset) \
108 __asm ( "mov %0, #0\n add %0, %0, #:tprel_hi12:" #var "\n add %0, %0, #:tprel_lo12_nc:" #var "\n" \
111 #elif defined(TARGET_ARM) && defined(__ARM_EABI__) && !defined(PIC)
113 #define MONO_THREAD_VAR_OFFSET(var,offset) __asm (" ldr %0, 1f; b 2f; 1: .word " #var "(tpoff); 2:" : "=r" (offset))
115 #elif defined(TARGET_S390X)
116 # if defined(__PIC__)
117 # if !defined(__PIE__)
118 // This only works if libmono is linked into the application
119 # define MONO_THREAD_VAR_OFFSET(var,offset) do { guint64 foo; \
120 __asm__ ("basr %%r1,0\n\t" \
122 ".quad " #var "@TLSGD\n" \
124 "lg %%r2,4(%%r1)\n\t" \
125 "brasl %%r14,__tls_get_offset@PLT:tls_gdcall:"#var"\n\t" \
128 : "1", "2", "14", "cc"); \
129 offset = foo; } while (0)
131 # define MONO_THREAD_VAR_OFFSET(var,offset) do { guint64 foo; \
132 __asm__ ("lg %0," #var "@GOTNTPOFF(%%r12)\n\t" \
134 offset = foo; } while (0)
136 # define MONO_THREAD_VAR_OFFSET(var,offset) do { guint64 foo; \
137 __asm__ ("larl %%r1," #var "@INDNTPOFF\n\t" \
138 "lg %0,0(%%r1)\n\t" \
141 offset = foo; } while (0)
144 # define MONO_THREAD_VAR_OFFSET(var,offset) do { guint64 foo; \
145 __asm__ ("basr %%r1,0\n\t" \
147 ".quad " #var "@NTPOFF\n" \
149 "lg %0,4(%%r1)\n\t" \
150 : "=r" (foo) : : "1"); \
151 offset = foo; } while (0)
155 #define MONO_THREAD_VAR_OFFSET(var,offset) (offset) = -1
159 /* Tls variables for each MonoTlsKey */
161 static __thread gpointer mono_tls_thread MONO_TLS_FAST;
162 static __thread gpointer mono_tls_jit_tls MONO_TLS_FAST;
163 static __thread gpointer mono_tls_domain MONO_TLS_FAST;
164 static __thread gpointer mono_tls_lmf MONO_TLS_FAST;
165 static __thread gpointer mono_tls_sgen_thread_info MONO_TLS_FAST;
166 static __thread gpointer mono_tls_lmf_addr MONO_TLS_FAST;
170 #if defined(TARGET_AMD64) && (defined(TARGET_MACH) || defined(HOST_WIN32))
171 #define MONO_THREAD_VAR_OFFSET(key,offset) (offset) = (gint32)key
172 #elif defined(TARGET_X86) && (defined(TARGET_MACH) || defined(HOST_WIN32))
173 #define MONO_THREAD_VAR_OFFSET(key,offset) (offset) = (gint32)key
175 #define MONO_THREAD_VAR_OFFSET(var,offset) (offset) = -1
178 static MonoNativeTlsKey mono_tls_key_thread;
179 static MonoNativeTlsKey mono_tls_key_jit_tls;
180 static MonoNativeTlsKey mono_tls_key_domain;
181 static MonoNativeTlsKey mono_tls_key_lmf;
182 static MonoNativeTlsKey mono_tls_key_sgen_thread_info;
183 static MonoNativeTlsKey mono_tls_key_lmf_addr;
187 static gint32 tls_offsets [TLS_KEY_NUM];
190 #define MONO_TLS_GET_VALUE(tls_var,tls_key) (tls_var)
191 #define MONO_TLS_SET_VALUE(tls_var,tls_key,value) (tls_var = value)
193 #define MONO_TLS_GET_VALUE(tls_var,tls_key) (mono_native_tls_get_value (tls_key))
194 #define MONO_TLS_SET_VALUE(tls_var,tls_key,value) (mono_native_tls_set_value (tls_key, value))
198 mono_tls_init_gc_keys (void)
201 MONO_THREAD_VAR_OFFSET (mono_tls_sgen_thread_info, tls_offsets [TLS_KEY_SGEN_THREAD_INFO]);
203 mono_native_tls_alloc (&mono_tls_key_sgen_thread_info, NULL);
204 MONO_THREAD_VAR_OFFSET (mono_tls_key_sgen_thread_info, tls_offsets [TLS_KEY_SGEN_THREAD_INFO]);
209 mono_tls_init_runtime_keys (void)
212 MONO_THREAD_VAR_OFFSET (mono_tls_thread, tls_offsets [TLS_KEY_THREAD]);
213 MONO_THREAD_VAR_OFFSET (mono_tls_jit_tls, tls_offsets [TLS_KEY_JIT_TLS]);
214 MONO_THREAD_VAR_OFFSET (mono_tls_domain, tls_offsets [TLS_KEY_DOMAIN]);
215 MONO_THREAD_VAR_OFFSET (mono_tls_lmf, tls_offsets [TLS_KEY_LMF]);
216 MONO_THREAD_VAR_OFFSET (mono_tls_lmf_addr, tls_offsets [TLS_KEY_LMF_ADDR]);
218 mono_native_tls_alloc (&mono_tls_key_thread, NULL);
219 MONO_THREAD_VAR_OFFSET (mono_tls_key_thread, tls_offsets [TLS_KEY_THREAD]);
220 mono_native_tls_alloc (&mono_tls_key_jit_tls, NULL);
221 MONO_THREAD_VAR_OFFSET (mono_tls_key_jit_tls, tls_offsets [TLS_KEY_JIT_TLS]);
222 mono_native_tls_alloc (&mono_tls_key_domain, NULL);
223 MONO_THREAD_VAR_OFFSET (mono_tls_key_domain, tls_offsets [TLS_KEY_DOMAIN]);
224 mono_native_tls_alloc (&mono_tls_key_lmf, NULL);
225 MONO_THREAD_VAR_OFFSET (mono_tls_key_lmf, tls_offsets [TLS_KEY_LMF]);
226 mono_native_tls_alloc (&mono_tls_key_lmf_addr, NULL);
227 MONO_THREAD_VAR_OFFSET (mono_tls_key_lmf_addr, tls_offsets [TLS_KEY_LMF_ADDR]);
232 mono_tls_free_keys (void)
234 #ifndef USE_KW_THREAD
235 mono_native_tls_free (mono_tls_key_thread);
236 mono_native_tls_free (mono_tls_key_jit_tls);
237 mono_native_tls_free (mono_tls_key_domain);
238 mono_native_tls_free (mono_tls_key_lmf);
239 mono_native_tls_free (mono_tls_key_sgen_thread_info);
240 mono_native_tls_free (mono_tls_key_lmf_addr);
246 * Gets the tls offset associated with the key. This offset is set at key
247 * initialization (at runtime). Certain targets can implement computing
248 * this offset and using it at runtime for fast inlined tls access.
251 mono_tls_get_tls_offset (MonoTlsKey key)
253 g_assert (tls_offsets [key]);
254 return tls_offsets [key];
258 * Returns the getter (gpointer (*)(void)) for the mono tls key.
259 * Managed code will always get the value by calling this getter.
262 mono_tls_get_tls_getter (MonoTlsKey key, gboolean name)
266 return name ? (gpointer)"mono_tls_get_thread" : (gpointer)mono_tls_get_thread;
267 case TLS_KEY_JIT_TLS:
268 return name ? (gpointer)"mono_tls_get_jit_tls" : (gpointer)mono_tls_get_jit_tls;
270 return name ? (gpointer)"mono_tls_get_domain" : (gpointer)mono_tls_get_domain;
272 return name ? (gpointer)"mono_tls_get_lmf" : (gpointer)mono_tls_get_lmf;
273 case TLS_KEY_SGEN_THREAD_INFO:
274 return name ? (gpointer)"mono_tls_get_sgen_thread_info" : (gpointer)mono_tls_get_sgen_thread_info;
275 case TLS_KEY_LMF_ADDR:
276 return name ? (gpointer)"mono_tls_get_lmf_addr" : (gpointer)mono_tls_get_lmf_addr;
278 g_assert_not_reached ();
282 /* Returns the setter (void (*)(gpointer)) for the mono tls key */
284 mono_tls_get_tls_setter (MonoTlsKey key, gboolean name)
288 return name ? (gpointer)"mono_tls_set_thread" : (gpointer)mono_tls_set_thread;
289 case TLS_KEY_JIT_TLS:
290 return name ? (gpointer)"mono_tls_set_jit_tls" : (gpointer)mono_tls_set_jit_tls;
292 return name ? (gpointer)"mono_tls_set_domain" : (gpointer)mono_tls_set_domain;
294 return name ? (gpointer)"mono_tls_set_lmf" : (gpointer)mono_tls_set_lmf;
295 case TLS_KEY_SGEN_THREAD_INFO:
296 return name ? (gpointer)"mono_tls_set_sgen_thread_info" : (gpointer)mono_tls_set_sgen_thread_info;
297 case TLS_KEY_LMF_ADDR:
298 return name ? (gpointer)"mono_tls_set_lmf_addr" : (gpointer)mono_tls_set_lmf_addr;
300 g_assert_not_reached ();
305 mono_tls_get_tls_addr (MonoTlsKey key)
307 #ifdef HAVE_GET_TLS_ADDR
308 if (key == TLS_KEY_LMF) {
309 #if defined(USE_KW_THREAD)
310 return &mono_tls_lmf;
311 #elif defined(TARGET_MACH)
312 return mono_mach_get_tls_address_from_thread (pthread_self (), mono_tls_key_lmf);
316 /* Implement if we ever need for other targets/keys */
317 g_assert_not_reached ();
321 /* Getters for each tls key */
322 gpointer mono_tls_get_thread (void)
324 return MONO_TLS_GET_VALUE (mono_tls_thread, mono_tls_key_thread);
327 gpointer mono_tls_get_jit_tls (void)
329 return MONO_TLS_GET_VALUE (mono_tls_jit_tls, mono_tls_key_jit_tls);
332 gpointer mono_tls_get_domain (void)
334 return MONO_TLS_GET_VALUE (mono_tls_domain, mono_tls_key_domain);
337 gpointer mono_tls_get_lmf (void)
339 return MONO_TLS_GET_VALUE (mono_tls_lmf, mono_tls_key_lmf);
342 gpointer mono_tls_get_sgen_thread_info (void)
344 return MONO_TLS_GET_VALUE (mono_tls_sgen_thread_info, mono_tls_key_sgen_thread_info);
347 gpointer mono_tls_get_lmf_addr (void)
349 return MONO_TLS_GET_VALUE (mono_tls_lmf_addr, mono_tls_key_lmf_addr);
352 /* Setters for each tls key */
353 void mono_tls_set_thread (gpointer value)
355 MONO_TLS_SET_VALUE (mono_tls_thread, mono_tls_key_thread, value);
358 void mono_tls_set_jit_tls (gpointer value)
360 MONO_TLS_SET_VALUE (mono_tls_jit_tls, mono_tls_key_jit_tls, value);
363 void mono_tls_set_domain (gpointer value)
365 MONO_TLS_SET_VALUE (mono_tls_domain, mono_tls_key_domain, value);
368 void mono_tls_set_lmf (gpointer value)
370 MONO_TLS_SET_VALUE (mono_tls_lmf, mono_tls_key_lmf, value);
373 void mono_tls_set_sgen_thread_info (gpointer value)
375 MONO_TLS_SET_VALUE (mono_tls_sgen_thread_info, mono_tls_key_sgen_thread_info, value);
378 void mono_tls_set_lmf_addr (gpointer value)
380 MONO_TLS_SET_VALUE (mono_tls_lmf_addr, mono_tls_key_lmf_addr, value);