[Facades] Use the Open.snk key for the System.ValueTuple facade (#4173)
[mono-project.git] / mono / mini / mini-trampolines.c
blob501e8a0ae9187b6030fe1c926992273a3ddc8c15
1 /*
2 * (C) 2003 Ximian, Inc.
3 * (C) 2003-2011 Novell, Inc.
4 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
5 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6 */
7 #include <config.h>
8 #include <glib.h>
10 #include <mono/metadata/appdomain.h>
11 #include <mono/metadata/metadata-internals.h>
12 #include <mono/metadata/marshal.h>
13 #include <mono/metadata/tabledefs.h>
14 #include <mono/utils/mono-counters.h>
15 #include <mono/utils/mono-error-internals.h>
16 #include <mono/utils/mono-membar.h>
17 #include <mono/utils/mono-compiler.h>
18 #include <mono/utils/mono-threads-coop.h>
20 #include "mini.h"
23 * Address of the trampoline code. This is used by the debugger to check
24 * whether a method is a trampoline.
26 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
28 static GHashTable *rgctx_lazy_fetch_trampoline_hash;
29 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr;
30 static guint32 trampoline_calls, jit_trampolines, unbox_trampolines, static_rgctx_trampolines;
32 #define mono_trampolines_lock() mono_os_mutex_lock (&trampolines_mutex)
33 #define mono_trampolines_unlock() mono_os_mutex_unlock (&trampolines_mutex)
34 static mono_mutex_t trampolines_mutex;
36 #ifdef MONO_ARCH_GSHARED_SUPPORTED
38 typedef struct {
39 MonoMethod *m;
40 gpointer addr;
41 } RgctxTrampInfo;
43 static gint
44 rgctx_tramp_info_equal (gconstpointer ka, gconstpointer kb)
46 const RgctxTrampInfo *i1 = (const RgctxTrampInfo *)ka;
47 const RgctxTrampInfo *i2 = (const RgctxTrampInfo *)kb;
49 if (i1->m == i2->m && i1->addr == i2->addr)
50 return 1;
51 else
52 return 0;
55 static guint
56 rgctx_tramp_info_hash (gconstpointer data)
58 const RgctxTrampInfo *info = (const RgctxTrampInfo *)data;
60 return GPOINTER_TO_UINT (info->m) ^ GPOINTER_TO_UINT (info->addr);
63 /**
64 * mono_create_static_rgctx_trampoline:
65 * @m: the mono method to create a trampoline for
66 * @addr: the address to jump to (where the compiled code for M lives)
68 * Creates a static rgctx trampoline for M which branches to ADDR which should
69 * point to the compiled code of M.
71 * Static rgctx trampolines are used when a shared generic method which doesn't
72 * have a this argument is called indirectly, ie. from code which can't pass in
73 * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
74 * methods code. These trampolines are similar to the unbox trampolines, they
75 * perform the same task as the static rgctx wrappers, but they are smaller/faster,
76 * and can be made to work with full AOT.
78 * On PPC addr should be an ftnptr and the return value is an ftnptr too.
80 * Returns the generated static rgctx trampoline.
82 gpointer
83 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
85 gpointer ctx;
86 gpointer res;
87 MonoDomain *domain;
88 RgctxTrampInfo tmp_info;
89 RgctxTrampInfo *info;
91 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
92 g_assert (((gpointer*)addr) [2] == 0);
93 #endif
95 ctx = mini_method_get_rgctx (m);
97 domain = mono_domain_get ();
99 /*
100 * In the AOT case, addr might point to either the method, or to an unbox trampoline,
101 * so make the hash keyed on the m+addr pair.
103 mono_domain_lock (domain);
104 if (!domain_jit_info (domain)->static_rgctx_trampoline_hash)
105 domain_jit_info (domain)->static_rgctx_trampoline_hash = g_hash_table_new (rgctx_tramp_info_hash, rgctx_tramp_info_equal);
106 tmp_info.m = m;
107 tmp_info.addr = addr;
108 res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
109 &tmp_info);
110 mono_domain_unlock (domain);
111 if (res)
112 return res;
114 if (mono_aot_only)
115 res = mono_aot_get_static_rgctx_trampoline (ctx, addr);
116 else
117 res = mono_arch_get_static_rgctx_trampoline (m, (MonoMethodRuntimeGenericContext *)ctx, addr);
119 mono_domain_lock (domain);
120 /* Duplicates inserted while we didn't hold the lock are OK */
121 info = (RgctxTrampInfo *)mono_domain_alloc (domain, sizeof (RgctxTrampInfo));
122 info->m = m;
123 info->addr = addr;
124 g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, info, res);
125 mono_domain_unlock (domain);
127 static_rgctx_trampolines ++;
129 return res;
131 #else
132 gpointer
133 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
136 * This shouldn't happen as all arches which support generic sharing support
137 * static rgctx trampolines as well.
139 g_assert_not_reached ();
141 #endif
143 #if 0
144 #define DEBUG_IMT(stmt) do { stmt; } while (0)
145 #else
146 #define DEBUG_IMT(stmt) do { } while (0)
147 #endif
150 * mini_resolve_imt_method:
152 * Resolve the actual method called when making an IMT call through VTABLE_SLOT with IMT_METHOD as the interface method.
154 * Either IMPL_METHOD or OUT_AOT_ADDR will be set on return.
156 gpointer*
157 mini_resolve_imt_method (MonoVTable *vt, gpointer *vtable_slot, MonoMethod *imt_method, MonoMethod **impl_method, gpointer *out_aot_addr, gboolean *out_need_rgctx_tramp, MonoMethod **variant_iface, MonoError *error)
159 MonoMethod *impl = NULL, *generic_virtual = NULL;
160 gboolean lookup_aot, variance_used = FALSE, need_rgctx_tramp = FALSE;
161 gpointer addr;
162 guint8 *aot_addr = NULL;
163 int displacement = vtable_slot - ((gpointer*)vt);
164 int interface_offset;
165 int imt_slot = MONO_IMT_SIZE + displacement;
167 g_assert (imt_slot < MONO_IMT_SIZE);
169 mono_error_init (error);
170 /* This has to be variance aware since imt_method can be from an interface that vt->klass doesn't directly implement */
171 interface_offset = mono_class_interface_offset_with_variance (vt->klass, imt_method->klass, &variance_used);
172 if (interface_offset < 0)
173 g_error ("%s doesn't implement interface %s\n", mono_type_get_name_full (&vt->klass->byval_arg, MONO_TYPE_NAME_FORMAT_IL), mono_type_get_name_full (&imt_method->klass->byval_arg, MONO_TYPE_NAME_FORMAT_IL));
175 *variant_iface = NULL;
176 if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
177 /* Generic virtual method */
178 generic_virtual = imt_method;
179 need_rgctx_tramp = TRUE;
180 } else if (variance_used && mono_class_has_variant_generic_params (imt_method->klass)) {
181 *variant_iface = imt_method;
184 addr = NULL;
185 /* We can only use the AOT compiled code if we don't require further processing */
186 lookup_aot = !generic_virtual & !variant_iface;
188 if (!mono_llvm_only)
189 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
191 if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
192 MonoGenericContext context = { NULL, NULL };
195 * Generic virtual method, imt_method contains the inflated interface
196 * method, need to get the inflated impl method.
198 /* imt_method->slot might not be set */
199 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
201 if (mono_class_is_ginst (impl->klass))
202 context.class_inst = mono_class_get_generic_class (impl->klass)->context.class_inst;
203 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
204 impl = mono_class_inflate_generic_method_checked (impl, &context, error);
205 mono_error_assert_ok (error);
206 } else {
208 /* Avoid loading metadata or creating a generic vtable if possible */
209 if (lookup_aot && !vt->klass->valuetype) {
210 aot_addr = (guint8 *)mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, interface_offset + mono_method_get_vtable_slot (imt_method), error);
211 return_val_if_nok (error, NULL);
212 } else {
213 aot_addr = NULL;
215 if (aot_addr)
216 impl = NULL;
217 else
218 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_vtable_slot (imt_method));
221 if (impl && mono_method_needs_static_rgctx_invoke (impl, FALSE))
222 need_rgctx_tramp = TRUE;
223 if (impl && impl->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
224 WrapperInfo *info = mono_marshal_get_wrapper_info (impl);
226 if (info && info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER)
227 need_rgctx_tramp = TRUE;
229 *impl_method = impl;
230 *out_need_rgctx_tramp = need_rgctx_tramp;
231 *out_aot_addr = aot_addr;
233 DEBUG_IMT (printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
234 method->klass->name_space, method->klass->name, method->name,
235 imt_method->klass->name_space, imt_method->klass->name, imt_method->name));
237 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
238 int slot = mono_method_get_vtable_index (imt_method);
239 int vtable_offset;
241 g_assert (slot != -1);
242 vtable_offset = interface_offset + slot;
243 vtable_slot = & (vt->vtable [vtable_offset]);
244 DEBUG_IMT (printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, and colliding becomes %p[%d] (interface_offset = %d, method->slot = %d)\n", slot, imt_slot, vtable_slot, vtable_offset, interface_offset, imt_method->slot));
245 return vtable_slot;
246 } else {
247 DEBUG_IMT (printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot));
248 return vtable_slot;
253 * This is a super-ugly hack to fix bug #616463.
255 * The problem is that we don't always set is_generic for generic
256 * method definitions. See the comment at the end of
257 * mono_class_inflate_generic_method_full_checked() in class.c.
259 static gboolean
260 is_generic_method_definition (MonoMethod *m)
262 MonoGenericContext *context;
263 if (m->is_generic)
264 return TRUE;
265 if (!m->is_inflated)
266 return FALSE;
268 context = mono_method_get_context (m);
269 if (!context->method_inst)
270 return FALSE;
271 if (context->method_inst == mono_method_get_generic_container (((MonoMethodInflated*)m)->declaring)->context.method_inst)
272 return TRUE;
273 return FALSE;
276 gboolean
277 mini_jit_info_is_gsharedvt (MonoJitInfo *ji)
279 if (ji && ji->has_generic_jit_info && (mono_jit_info_get_generic_sharing_context (ji)->is_gsharedvt))
280 return TRUE;
281 else
282 return FALSE;
286 * mini_add_method_trampoline:
287 * @m:
288 * @compiled_method:
289 * @add_static_rgctx_tramp: adds a static rgctx trampoline
290 * @add_unbox_tramp: adds an unboxing trampoline
292 * Add static rgctx/gsharedvt_in/unbox trampolines to
293 * M/COMPILED_METHOD if needed.
295 * Returns the trampoline address, or COMPILED_METHOD if no trampoline
296 * is needed.
298 gpointer
299 mini_add_method_trampoline (MonoMethod *m, gpointer compiled_method, gboolean add_static_rgctx_tramp, gboolean add_unbox_tramp)
301 gpointer addr = compiled_method;
302 gboolean callee_gsharedvt, callee_array_helper;
303 MonoMethod *jmethod = NULL;
304 MonoJitInfo *ji;
306 // FIXME: This loads information from AOT (perf problem)
307 ji = mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method), NULL);
308 callee_gsharedvt = mini_jit_info_is_gsharedvt (ji);
310 callee_array_helper = FALSE;
311 if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
312 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
315 * generic array helpers.
316 * Have to replace the wrappers with the original generic instances.
318 if (info && info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER) {
319 callee_array_helper = TRUE;
320 m = info->d.generic_array_helper.method;
322 } else if (m->wrapper_type == MONO_WRAPPER_UNKNOWN) {
323 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
325 /* Same for synchronized inner wrappers */
326 if (info && info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
327 m = info->d.synchronized_inner.method;
331 if (callee_gsharedvt)
332 g_assert (m->is_inflated);
334 addr = compiled_method;
336 if (add_unbox_tramp) {
338 * The unbox trampolines call the method directly, so need to add
339 * an rgctx tramp before them.
341 if (mono_aot_only) {
342 addr = mono_aot_get_unbox_trampoline (m);
343 } else {
344 unbox_trampolines ++;
345 addr = mono_arch_get_unbox_trampoline (m, addr);
349 if (ji && !ji->is_trampoline)
350 jmethod = jinfo_get_method (ji);
351 if (callee_gsharedvt && mini_is_gsharedvt_variable_signature (mono_method_signature (jmethod))) {
352 MonoMethodSignature *sig, *gsig;
354 /* Here m is a generic instance, while ji->method is the gsharedvt method implementing it */
356 /* Call from normal/gshared code to gsharedvt code with variable signature */
357 sig = mono_method_signature (m);
358 gsig = mono_method_signature (jmethod);
360 addr = mini_get_gsharedvt_wrapper (TRUE, addr, sig, gsig, -1, FALSE);
362 if (mono_llvm_only)
363 g_assert_not_reached ();
364 //printf ("IN: %s\n", mono_method_full_name (m, TRUE));
367 if (callee_array_helper) {
368 add_static_rgctx_tramp = FALSE;
369 /* In AOT mode, compiled_method points to one of the InternalArray methods in Array. */
370 if (ji && !mono_llvm_only && mono_method_needs_static_rgctx_invoke (jinfo_get_method (ji), TRUE))
371 add_static_rgctx_tramp = TRUE;
374 if (mono_llvm_only)
375 add_static_rgctx_tramp = FALSE;
377 if (add_static_rgctx_tramp)
378 addr = mono_create_static_rgctx_trampoline (m, addr);
380 return addr;
384 * mini_create_llvmonly_ftndesc:
386 * Create a function descriptor of the form <addr, arg>, which
387 * represents a callee ADDR with ARG as the last argument.
388 * This is used for:
389 * - generic sharing (ARG is the rgctx)
390 * - gsharedvt signature wrappers (ARG is a function descriptor)
392 MonoFtnDesc*
393 mini_create_llvmonly_ftndesc (MonoDomain *domain, gpointer addr, gpointer arg)
395 MonoFtnDesc *ftndesc = (MonoFtnDesc*)mono_domain_alloc0 (mono_domain_get (), 2 * sizeof (gpointer));
396 ftndesc->addr = addr;
397 ftndesc->arg = arg;
399 return ftndesc;
403 * mini_add_method_wrappers_llvmonly:
405 * Add unbox/gsharedvt wrappers around COMPILED_METHOD if needed. Return the wrapper address or COMPILED_METHOD
406 * if no wrapper is needed. Set OUT_ARG to the rgctx/extra argument needed to be passed to the returned method.
408 gpointer
409 mini_add_method_wrappers_llvmonly (MonoMethod *m, gpointer compiled_method, gboolean caller_gsharedvt, gboolean add_unbox_tramp, gpointer *out_arg)
411 gpointer addr;
412 gboolean callee_gsharedvt, callee_array_helper;
413 MonoMethod *jmethod = NULL;
414 MonoJitInfo *ji;
416 // FIXME: This loads information from AOT (perf problem)
417 ji = mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method), NULL);
418 callee_gsharedvt = mini_jit_info_is_gsharedvt (ji);
420 callee_array_helper = FALSE;
421 if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
422 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
425 * generic array helpers.
426 * Have to replace the wrappers with the original generic instances.
428 if (info && info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER) {
429 callee_array_helper = TRUE;
430 m = info->d.generic_array_helper.method;
432 } else if (m->wrapper_type == MONO_WRAPPER_UNKNOWN) {
433 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
435 /* Same for synchronized inner wrappers */
436 if (info && info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
437 m = info->d.synchronized_inner.method;
441 if (callee_gsharedvt)
442 g_assert (m->is_inflated);
444 addr = compiled_method;
446 if (add_unbox_tramp) {
448 * The unbox trampolines call the method directly, so need to add
449 * an rgctx tramp before them.
451 if (mono_aot_only) {
452 addr = mono_aot_get_unbox_trampoline (m);
453 } else {
454 unbox_trampolines ++;
455 addr = mono_arch_get_unbox_trampoline (m, addr);
459 g_assert (mono_llvm_only);
460 g_assert (out_arg);
462 if (ji && !ji->is_trampoline)
463 jmethod = jinfo_get_method (ji);
465 if (callee_gsharedvt)
466 callee_gsharedvt = mini_is_gsharedvt_variable_signature (mono_method_signature (jmethod));
468 if (!caller_gsharedvt && callee_gsharedvt) {
469 MonoMethodSignature *sig, *gsig;
470 gpointer wrapper_addr;
472 /* Here m is a generic instance, while ji->method is the gsharedvt method implementing it */
474 /* Call from normal/gshared code to gsharedvt code with variable signature */
475 sig = mono_method_signature (m);
476 gsig = mono_method_signature (jmethod);
478 wrapper_addr = mini_get_gsharedvt_wrapper (TRUE, addr, sig, gsig, -1, FALSE);
481 * This is a gsharedvt in wrapper, it gets passed a ftndesc for the gsharedvt method as an argument.
483 *out_arg = mini_create_llvmonly_ftndesc (mono_domain_get (), addr, mini_method_get_rgctx (m));
484 addr = wrapper_addr;
485 //printf ("IN: %s\n", mono_method_full_name (m, TRUE));
488 if (!(*out_arg) && mono_method_needs_static_rgctx_invoke (m, FALSE))
489 *out_arg = mini_method_get_rgctx (m);
491 if (caller_gsharedvt && !callee_gsharedvt) {
493 * The callee uses the gsharedvt calling convention, have to add an out wrapper.
495 gpointer out_wrapper = mini_get_gsharedvt_wrapper (FALSE, NULL, mono_method_signature (m), NULL, -1, FALSE);
496 MonoFtnDesc *out_wrapper_arg = mini_create_llvmonly_ftndesc (mono_domain_get (), addr, *out_arg);
498 addr = out_wrapper;
499 *out_arg = out_wrapper_arg;
502 return addr;
506 * common_call_trampoline:
508 * The code to handle normal, virtual, and interface method calls and jumps, both
509 * from JITted and LLVM compiled code.
511 static gpointer
512 common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, MonoVTable *vt, gpointer *vtable_slot, MonoError *error)
514 gpointer addr, compiled_method;
515 gboolean generic_shared = FALSE;
516 gboolean need_unbox_tramp = FALSE;
517 gboolean need_rgctx_tramp = FALSE;
518 MonoMethod *declaring = NULL;
519 MonoMethod *generic_virtual = NULL, *variant_iface = NULL;
520 int context_used;
521 gboolean imt_call, virtual_;
522 gpointer *orig_vtable_slot, *vtable_slot_to_patch = NULL;
523 MonoJitInfo *ji = NULL;
525 mono_error_init (error);
527 virtual_ = vt && (gpointer)vtable_slot > (gpointer)vt;
528 imt_call = vt && (gpointer)vtable_slot < (gpointer)vt;
531 * rgctx trampolines are needed when the call is indirect so the caller can't pass
532 * the rgctx argument needed by the callee.
534 if (virtual_ && m)
535 need_rgctx_tramp = mono_method_needs_static_rgctx_invoke (m, FALSE);
537 orig_vtable_slot = vtable_slot;
538 vtable_slot_to_patch = vtable_slot;
540 /* IMT call */
541 if (imt_call) {
542 MonoMethod *imt_method = NULL, *impl_method = NULL;
543 MonoObject *this_arg;
545 g_assert (vtable_slot);
547 imt_method = mono_arch_find_imt_method (regs, code);
548 this_arg = (MonoObject *)mono_arch_get_this_arg_from_call (regs, code);
550 if (mono_object_is_transparent_proxy (this_arg)) {
551 /* Use the slow path for now */
552 m = mono_object_get_virtual_method (this_arg, imt_method);
553 vtable_slot_to_patch = NULL;
554 } else {
555 if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
556 /* Generic virtual method */
557 generic_virtual = imt_method;
558 need_rgctx_tramp = TRUE;
561 vtable_slot = mini_resolve_imt_method (vt, vtable_slot, imt_method, &impl_method, &addr, &need_rgctx_tramp, &variant_iface, error);
562 return_val_if_nok (error, NULL);
564 /* This is the vcall slot which gets called through the IMT trampoline */
565 vtable_slot_to_patch = vtable_slot;
567 if (addr) {
569 * We found AOT compiled code for the method, skip the rest.
571 if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
572 *vtable_slot = addr;
574 return mono_create_ftnptr (mono_domain_get (), addr);
577 m = impl_method;
582 * The virtual check is needed because is_generic_method_definition (m) could
583 * return TRUE for methods used in IMT calls too.
585 if (virtual_ && is_generic_method_definition (m)) {
586 MonoGenericContext context = { NULL, NULL };
587 MonoMethod *declaring;
589 if (m->is_inflated)
590 declaring = mono_method_get_declaring_generic_method (m);
591 else
592 declaring = m;
594 if (mono_class_is_ginst (m->klass))
595 context.class_inst = mono_class_get_generic_class (m->klass)->context.class_inst;
596 else
597 g_assert (!mono_class_is_gtd (m->klass));
599 generic_virtual = mono_arch_find_imt_method (regs, code);
600 g_assert (generic_virtual);
601 g_assert (generic_virtual->is_inflated);
602 context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
604 m = mono_class_inflate_generic_method_checked (declaring, &context, error);
605 mono_error_assert_ok (error);
606 /* FIXME: only do this if the method is sharable */
607 need_rgctx_tramp = TRUE;
608 } else if ((context_used = mono_method_check_context_used (m))) {
609 MonoClass *klass = NULL;
610 MonoMethod *actual_method = NULL;
611 MonoVTable *vt = NULL;
612 MonoGenericInst *method_inst = NULL;
614 vtable_slot = NULL;
615 generic_shared = TRUE;
617 g_assert (code);
620 * The caller is gshared code, compute the actual method to call from M and this/rgctx.
622 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
623 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable (regs, code);
625 klass = mrgctx->class_vtable->klass;
626 method_inst = mrgctx->method_inst;
627 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
628 MonoVTable *vtable = mono_arch_find_static_call_vtable (regs, code);
630 klass = vtable->klass;
631 } else {
632 MonoObject *this_argument = (MonoObject *)mono_arch_get_this_arg_from_call (regs, code);
634 vt = this_argument->vtable;
635 vtable_slot = orig_vtable_slot;
637 g_assert (this_argument->vtable->klass->inited);
639 if (!vtable_slot) {
640 mono_class_setup_supertypes (this_argument->vtable->klass);
641 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
645 g_assert (vtable_slot || klass);
647 if (vtable_slot) {
648 int displacement = vtable_slot - ((gpointer*)vt);
650 g_assert_not_reached ();
652 g_assert (displacement > 0);
654 actual_method = vt->klass->vtable [displacement];
657 if (method_inst || m->wrapper_type) {
658 MonoGenericContext context = { NULL, NULL };
660 if (m->is_inflated)
661 declaring = mono_method_get_declaring_generic_method (m);
662 else
663 declaring = m;
665 if (mono_class_is_ginst (klass))
666 context.class_inst = mono_class_get_generic_class (klass)->context.class_inst;
667 else if (mono_class_is_gtd (klass))
668 context.class_inst = mono_class_get_generic_container (klass)->context.class_inst;
669 context.method_inst = method_inst;
671 actual_method = mono_class_inflate_generic_method_checked (declaring, &context, error);
672 mono_error_assert_ok (error);
673 } else {
674 actual_method = mono_class_get_method_generic (klass, m);
677 g_assert (klass);
678 g_assert (actual_method);
679 g_assert (actual_method->klass == klass);
681 if (actual_method->is_inflated)
682 declaring = mono_method_get_declaring_generic_method (actual_method);
683 else
684 declaring = NULL;
686 m = actual_method;
689 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
690 m = mono_marshal_get_synchronized_wrapper (m);
691 need_rgctx_tramp = FALSE;
694 /* Calls made through delegates on platforms without delegate trampolines */
695 if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
696 need_rgctx_tramp = TRUE;
698 addr = compiled_method = mono_jit_compile_method (m, error);
699 if (!addr)
700 return NULL;
702 if (generic_virtual || variant_iface) {
703 if (vt->klass->valuetype) /*FIXME is this required variant iface?*/
704 need_unbox_tramp = TRUE;
705 } else if (orig_vtable_slot) {
706 if (m->klass->valuetype)
707 need_unbox_tramp = TRUE;
710 addr = mini_add_method_trampoline (m, compiled_method, need_rgctx_tramp, need_unbox_tramp);
712 if (generic_virtual || variant_iface) {
713 MonoMethod *target = generic_virtual ? generic_virtual : variant_iface;
715 vtable_slot = orig_vtable_slot;
716 g_assert (vtable_slot);
718 mono_method_add_generic_virtual_invocation (mono_domain_get (),
719 vt, vtable_slot,
720 target, addr);
722 return addr;
725 /* the method was jumped to */
726 if (!code) {
727 MonoDomain *domain = mono_domain_get ();
729 /* Patch the got entries pointing to this method */
731 * We do this here instead of in mono_codegen () to cover the case when m
732 * was loaded from an aot image.
734 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
735 GSList *list, *tmp;
737 mono_domain_lock (domain);
738 list = (GSList *)g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
739 if (list) {
740 for (tmp = list; tmp; tmp = tmp->next) {
741 gpointer *got_slot = (gpointer *)tmp->data;
742 *got_slot = addr;
744 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
745 g_slist_free (list);
747 mono_domain_unlock (domain);
750 return addr;
753 vtable_slot = orig_vtable_slot;
755 if (vtable_slot) {
756 if (vtable_slot_to_patch && (mono_aot_is_got_entry (code, (guint8*)vtable_slot_to_patch) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot_to_patch))) {
757 g_assert (*vtable_slot_to_patch);
758 *vtable_slot_to_patch = mono_get_addr_from_ftnptr (addr);
760 } else {
761 guint8 *plt_entry = mono_aot_get_plt_entry (code);
762 gboolean no_patch = FALSE;
763 MonoJitInfo *target_ji;
765 if (plt_entry) {
766 if (generic_shared) {
767 target_ji =
768 mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method), NULL);
769 if (!ji)
770 ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
772 if (ji && target_ji && generic_shared && ji->has_generic_jit_info && !target_ji->has_generic_jit_info) {
773 no_patch = TRUE;
776 if (!no_patch)
777 mono_aot_patch_plt_entry (code, plt_entry, NULL, regs, (guint8 *)addr);
778 } else {
779 if (generic_shared) {
780 if (m->wrapper_type != MONO_WRAPPER_NONE)
781 m = mono_marshal_method_from_wrapper (m);
782 //g_assert (mono_method_is_generic_sharable (m, FALSE));
785 /* Patch calling code */
786 target_ji =
787 mini_jit_info_table_find (mono_domain_get (), (char *)mono_get_addr_from_ftnptr (compiled_method), NULL);
788 if (!ji)
789 ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
791 if (ji && target_ji && generic_shared && ji->has_generic_jit_info && !target_ji->has_generic_jit_info) {
793 * Can't patch the call as the caller is gshared, but the callee is not. Happens when
794 * generic sharing fails.
795 * FIXME: Performance problem.
797 no_patch = TRUE;
799 #if LLVM_API_VERSION > 100
800 /* LLVM code doesn't make direct calls */
801 if (ji && ji->from_llvm)
802 no_patch = TRUE;
803 #endif
804 if (!no_patch && mono_method_same_domain (ji, target_ji))
805 mono_arch_patch_callsite ((guint8 *)ji->code_start, code, (guint8 *)addr);
809 return addr;
813 * mono_magic_trampoline:
815 * This trampoline handles normal calls from JITted code.
817 gpointer
818 mono_magic_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp)
820 gpointer res;
822 MONO_ENTER_GC_UNSAFE_UNBALANCED;
824 MonoError error;
826 trampoline_calls ++;
828 res = common_call_trampoline (regs, code, (MonoMethod *)arg, NULL, NULL, &error);
829 mono_error_set_pending_exception (&error);
831 mono_interruption_checkpoint_from_trampoline ();
833 MONO_EXIT_GC_UNSAFE_UNBALANCED;
835 return res;
839 * mono_vcall_trampoline:
841 * This trampoline handles virtual calls.
843 static gpointer
844 mono_vcall_trampoline (mgreg_t *regs, guint8 *code, int slot, guint8 *tramp)
846 MONO_REQ_GC_UNSAFE_MODE;
848 MonoObject *this_arg;
849 MonoVTable *vt;
850 gpointer *vtable_slot;
851 MonoMethod *m;
852 MonoError error;
853 gpointer addr, res = NULL;
855 trampoline_calls ++;
858 * We need to obtain the following pieces of information:
859 * - the method which needs to be compiled.
860 * - the vtable slot.
861 * We use one vtable trampoline per vtable slot index, so we need only the vtable,
862 * the other two can be computed from the vtable + the slot index.
866 * Obtain the vtable from the 'this' arg.
868 this_arg = (MonoObject *)mono_arch_get_this_arg_from_call (regs, code);
869 g_assert (this_arg);
871 vt = this_arg->vtable;
873 if (slot >= 0) {
874 /* Normal virtual call */
875 vtable_slot = &(vt->vtable [slot]);
877 /* Avoid loading metadata or creating a generic vtable if possible */
878 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, slot, &error);
879 if (!is_ok (&error))
880 goto leave;
881 if (addr && !vt->klass->valuetype) {
882 if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
883 *vtable_slot = addr;
885 return mono_create_ftnptr (mono_domain_get (), addr);
889 * Bug #616463 (see
890 * is_generic_method_definition() above) also
891 * goes away if we do a
892 * mono_class_setup_vtable (vt->klass) here,
893 * because we then inflate the method
894 * correctly, put it in the cache, and the
895 * "wrong" inflation invocation still looks up
896 * the correctly inflated method.
898 * The hack above seems more stable and
899 * trustworthy.
901 m = mono_class_get_vtable_entry (vt->klass, slot);
902 } else {
903 /* IMT call */
904 vtable_slot = &(((gpointer*)vt) [slot]);
906 m = NULL;
909 res = common_call_trampoline (regs, code, m, vt, vtable_slot, &error);
910 leave:
911 if (!mono_error_ok (&error)) {
912 mono_error_set_pending_exception (&error);
913 return NULL;
915 return res;
918 #ifndef DISABLE_REMOTING
919 gpointer
920 mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
922 MONO_REQ_GC_UNSAFE_MODE;
924 MonoError error;
925 MonoGenericContext context = { NULL, NULL };
926 MonoMethod *imt_method, *declaring;
927 gpointer addr;
929 trampoline_calls ++;
931 g_assert (m->is_generic);
933 if (m->is_inflated)
934 declaring = mono_method_get_declaring_generic_method (m);
935 else
936 declaring = m;
938 if (mono_class_is_ginst (m->klass))
939 context.class_inst = mono_class_get_generic_class (m->klass)->context.class_inst;
940 else
941 g_assert (!mono_class_is_gtd (m->klass));
943 imt_method = mono_arch_find_imt_method (regs, code);
944 if (imt_method->is_inflated)
945 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
946 m = mono_class_inflate_generic_method_checked (declaring, &context, &error);
947 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */;
948 m = mono_marshal_get_remoting_invoke_with_check (m);
950 addr = mono_jit_compile_method (m, &error);
951 if (!mono_error_ok (&error)) {
952 mono_error_set_pending_exception (&error);
953 return NULL;
955 g_assert (addr);
957 return addr;
959 #endif
962 * mono_aot_trampoline:
964 * This trampoline handles calls made from AOT code. We try to bypass the
965 * normal JIT compilation logic to avoid loading the metadata for the method.
967 #ifdef MONO_ARCH_AOT_SUPPORTED
968 gpointer
969 mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info,
970 guint8* tramp)
972 MONO_REQ_GC_UNSAFE_MODE;
974 MonoImage *image;
975 guint32 token;
976 MonoMethod *method = NULL;
977 gpointer addr;
978 guint8 *plt_entry;
979 MonoError error;
981 trampoline_calls ++;
983 image = (MonoImage *)*(gpointer*)(gpointer)token_info;
984 token_info += sizeof (gpointer);
985 token = *(guint32*)(gpointer)token_info;
987 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token, &error);
988 if (!is_ok (&error))
989 mono_error_cleanup (&error);
990 if (!addr) {
991 method = mono_get_method_checked (image, token, NULL, NULL, &error);
992 if (!method)
993 g_error ("Could not load AOT trampoline due to %s", mono_error_get_message (&error));
995 /* Use the generic code */
996 return mono_magic_trampoline (regs, code, method, tramp);
999 addr = mono_create_ftnptr (mono_domain_get (), addr);
1001 /* This is a normal call through a PLT entry */
1002 plt_entry = mono_aot_get_plt_entry (code);
1003 g_assert (plt_entry);
1005 mono_aot_patch_plt_entry (code, plt_entry, NULL, regs, (guint8 *)addr);
1007 return addr;
1011 * mono_aot_plt_trampoline:
1013 * This trampoline handles calls made from AOT code through the PLT table.
1015 gpointer
1016 mono_aot_plt_trampoline (mgreg_t *regs, guint8 *code, guint8 *aot_module,
1017 guint8* tramp)
1019 MONO_REQ_GC_UNSAFE_MODE;
1021 guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
1022 gpointer res;
1023 MonoError error;
1025 trampoline_calls ++;
1027 res = mono_aot_plt_resolve (aot_module, plt_info_offset, code, &error);
1028 if (!res) {
1029 if (!mono_error_ok (&error)) {
1030 mono_error_set_pending_exception (&error);
1031 return NULL;
1033 // FIXME: Error handling (how ?)
1034 g_assert (res);
1037 return res;
1039 #endif
1041 static gpointer
1042 mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, guint8 *tramp)
1044 MONO_REQ_GC_UNSAFE_MODE;
1046 static gboolean inited = FALSE;
1047 static int num_lookups = 0;
1048 guint32 slot = GPOINTER_TO_UINT (data);
1049 mgreg_t *r = (mgreg_t*)regs;
1050 gpointer arg = (gpointer)(gssize)r [MONO_ARCH_VTABLE_REG];
1051 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
1052 gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
1053 MonoError error;
1054 gpointer res;
1056 trampoline_calls ++;
1058 if (!inited) {
1059 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
1060 inited = TRUE;
1063 num_lookups++;
1065 if (mrgctx)
1066 res = mono_method_fill_runtime_generic_context ((MonoMethodRuntimeGenericContext *)arg, index, &error);
1067 else
1068 res = mono_class_fill_runtime_generic_context ((MonoVTable *)arg, index, &error);
1069 if (!mono_error_ok (&error)) {
1070 mono_error_set_pending_exception (&error);
1071 return NULL;
1073 return res;
1077 * mono_delegate_trampoline:
1079 * This trampoline handles calls made to Delegate:Invoke ().
1080 * This is called once the first time a delegate is invoked, so it must be fast.
1082 gpointer
1083 mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *arg, guint8* tramp)
1085 MONO_REQ_GC_UNSAFE_MODE;
1087 MonoDomain *domain = mono_domain_get ();
1088 MonoDelegate *delegate;
1089 MonoJitInfo *ji;
1090 MonoMethod *m;
1091 MonoMethod *method = NULL;
1092 MonoError error;
1093 gboolean multicast, callvirt = FALSE, closed_over_null = FALSE;
1094 gboolean need_rgctx_tramp = FALSE;
1095 gboolean need_unbox_tramp = FALSE;
1096 gboolean enable_caching = TRUE;
1097 MonoDelegateTrampInfo *tramp_info = (MonoDelegateTrampInfo*)arg;
1098 MonoMethod *invoke = tramp_info->invoke;
1099 guint8 *impl_this = (guint8 *)tramp_info->impl_this;
1100 guint8 *impl_nothis = (guint8 *)tramp_info->impl_nothis;
1101 MonoError err;
1102 MonoMethodSignature *sig;
1103 gpointer addr, compiled_method;
1104 gboolean is_remote = FALSE;
1106 trampoline_calls ++;
1108 /* Obtain the delegate object according to the calling convention */
1109 delegate = (MonoDelegate *)mono_arch_get_this_arg_from_call (regs, code);
1110 g_assert (mono_class_has_parent (mono_object_class (delegate), mono_defaults.multicastdelegate_class));
1112 if (delegate->method) {
1113 method = delegate->method;
1116 * delegate->method_ptr == NULL means the delegate was initialized by
1117 * mini_delegate_ctor, while != NULL means it is initialized by
1118 * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
1119 * (ctor_with_method () does this, but it doesn't store the wrapper back into
1120 * delegate->method).
1122 #ifndef DISABLE_REMOTING
1123 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class) {
1124 is_remote = TRUE;
1125 #ifndef DISABLE_COM
1126 if (((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class != mono_class_get_com_object_class () &&
1127 !mono_class_is_com_object (((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class))
1128 #endif
1129 method = mono_marshal_get_remoting_invoke (method);
1131 #endif
1132 if (!is_remote) {
1133 sig = tramp_info->sig;
1134 if (!(sig && method == tramp_info->method)) {
1135 mono_error_init (&err);
1136 sig = mono_method_signature_checked (method, &err);
1137 if (!sig) {
1138 mono_error_set_pending_exception (&err);
1139 return NULL;
1143 if (sig->hasthis && method->klass->valuetype) {
1144 gboolean need_unbox = TRUE;
1146 if (tramp_info->invoke_sig->param_count > sig->param_count && tramp_info->invoke_sig->params [0]->byref)
1147 need_unbox = FALSE;
1149 if (need_unbox) {
1150 if (mono_aot_only)
1151 need_unbox_tramp = TRUE;
1152 else
1153 method = mono_marshal_get_unbox_wrapper (method);
1157 // If "delegate->method_ptr" is null mono_get_addr_from_ftnptr will fail if
1158 // ftnptrs are being used. "method" would end up null on archtitectures without
1159 // ftnptrs so we can just skip this.
1160 } else if (delegate->method_ptr) {
1161 ji = mono_jit_info_table_find (domain, (char *)mono_get_addr_from_ftnptr (delegate->method_ptr));
1162 if (ji)
1163 method = jinfo_get_method (ji);
1166 if (method) {
1167 sig = tramp_info->sig;
1168 if (!(sig && method == tramp_info->method)) {
1169 mono_error_init (&err);
1170 sig = mono_method_signature_checked (method, &err);
1171 if (!sig) {
1172 mono_error_set_pending_exception (&err);
1173 return NULL;
1177 callvirt = !delegate->target && sig->hasthis;
1178 if (callvirt)
1179 closed_over_null = tramp_info->invoke_sig->param_count == sig->param_count;
1181 if (callvirt && !closed_over_null) {
1183 * The delegate needs to make a virtual call to the target method using its
1184 * first argument as the receiver. This is hard to support in full-aot, so
1185 * optimize it in some cases if possible.
1186 * If the target method is not virtual or is in a sealed class,
1187 * the vcall will call it directly.
1188 * If the call doesn't return a valuetype, then the vcall uses the same calling
1189 * convention as a normal call.
1191 if ((mono_class_is_sealed (method->klass) || !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)) && !MONO_TYPE_ISSTRUCT (sig->ret)) {
1192 callvirt = FALSE;
1193 enable_caching = FALSE;
1197 if (delegate->target &&
1198 method->flags & METHOD_ATTRIBUTE_VIRTUAL &&
1199 method->flags & METHOD_ATTRIBUTE_ABSTRACT &&
1200 mono_class_is_abstract (method->klass)) {
1201 method = mono_object_get_virtual_method (delegate->target, method);
1202 enable_caching = FALSE;
1205 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1206 method = mono_marshal_get_synchronized_wrapper (method);
1208 if (method == tramp_info->method)
1209 need_rgctx_tramp = tramp_info->need_rgctx_tramp;
1210 else if (mono_method_needs_static_rgctx_invoke (method, FALSE))
1211 need_rgctx_tramp = TRUE;
1215 * If the called address is a trampoline, replace it with the compiled method so
1216 * further calls don't have to go through the trampoline.
1218 if (method && !callvirt) {
1219 /* Avoid the overhead of looking up an already compiled method if possible */
1220 if (enable_caching && delegate->method_code && *delegate->method_code) {
1221 delegate->method_ptr = *delegate->method_code;
1222 } else {
1223 compiled_method = addr = mono_jit_compile_method (method, &error);
1224 if (!mono_error_ok (&error)) {
1225 mono_error_set_pending_exception (&error);
1226 return NULL;
1228 addr = mini_add_method_trampoline (method, compiled_method, need_rgctx_tramp, need_unbox_tramp);
1229 delegate->method_ptr = addr;
1230 if (enable_caching && delegate->method_code)
1231 *delegate->method_code = (guint8 *)delegate->method_ptr;
1233 } else {
1234 if (need_rgctx_tramp)
1235 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
1238 /* Necessary for !code condition to fallback to slow path */
1239 code = NULL;
1241 multicast = ((MonoMulticastDelegate*)delegate)->delegates != NULL;
1242 if (!multicast && !callvirt) {
1243 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
1244 /* Closed static delegate */
1245 code = impl_this;
1246 else
1247 code = delegate->target ? impl_this : impl_nothis;
1250 if (!code) {
1251 /* The general, unoptimized case */
1252 m = mono_marshal_get_delegate_invoke (invoke, delegate);
1253 code = (guint8 *)mono_jit_compile_method (m, &error);
1254 if (!mono_error_ok (&error)) {
1255 mono_error_set_pending_exception (&error);
1256 return NULL;
1258 code = (guint8 *)mini_add_method_trampoline (m, code, mono_method_needs_static_rgctx_invoke (m, FALSE), FALSE);
1261 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
1262 if (enable_caching && !callvirt && tramp_info->method) {
1263 tramp_info->method_ptr = delegate->method_ptr;
1264 tramp_info->invoke_impl = delegate->invoke_impl;
1267 return code;
1270 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1271 static gpointer
1272 mono_handler_block_guard_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_info, guint8* tramp)
1274 MONO_REQ_GC_UNSAFE_MODE;
1276 MonoContext ctx;
1277 MonoException *exc;
1278 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_tls_get_jit_tls ();
1279 gpointer resume_ip = jit_tls->handler_block_return_address;
1281 memcpy (&ctx, &jit_tls->handler_block_context, sizeof (MonoContext));
1282 MONO_CONTEXT_SET_IP (&ctx, jit_tls->handler_block_return_address);
1284 jit_tls->handler_block_return_address = NULL;
1285 jit_tls->handler_block = NULL;
1287 if (!resume_ip) /*this should not happen, but we should avoid crashing */
1288 exc = mono_get_exception_execution_engine ("Invalid internal state, resuming abort after handler block but no resume ip found");
1289 else
1290 exc = mono_thread_resume_interruption ();
1292 if (exc) {
1293 mono_handle_exception (&ctx, (MonoObject *)exc);
1294 mono_restore_context (&ctx);
1297 return resume_ip;
1300 gpointer
1301 mono_create_handler_block_trampoline (void)
1303 static gpointer code;
1305 if (code)
1306 return code;
1308 if (mono_aot_only) {
1309 gpointer tmp = mono_aot_get_trampoline ("handler_block_trampoline");
1310 g_assert (tmp);
1311 mono_memory_barrier ();
1312 code = tmp;
1313 return code;
1316 mono_trampolines_lock ();
1317 if (!code) {
1318 MonoTrampInfo *info;
1319 gpointer tmp;
1321 tmp = mono_arch_create_handler_block_trampoline (&info, FALSE);
1322 mono_tramp_info_register (info, NULL);
1323 mono_memory_barrier ();
1324 code = tmp;
1326 mono_trampolines_unlock ();
1328 return code;
1330 #endif
1333 * mono_get_trampoline_func:
1335 * Return the C function which needs to be called by the generic trampoline of type
1336 * TRAMP_TYPE.
1338 gconstpointer
1339 mono_get_trampoline_func (MonoTrampolineType tramp_type)
1341 switch (tramp_type) {
1342 case MONO_TRAMPOLINE_JIT:
1343 case MONO_TRAMPOLINE_JUMP:
1344 return mono_magic_trampoline;
1345 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
1346 return mono_rgctx_lazy_fetch_trampoline;
1347 #ifdef MONO_ARCH_AOT_SUPPORTED
1348 case MONO_TRAMPOLINE_AOT:
1349 return mono_aot_trampoline;
1350 case MONO_TRAMPOLINE_AOT_PLT:
1351 return mono_aot_plt_trampoline;
1352 #endif
1353 case MONO_TRAMPOLINE_DELEGATE:
1354 return mono_delegate_trampoline;
1355 case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
1356 return mono_altstack_restore_prot;
1357 #ifndef DISABLE_REMOTING
1358 case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
1359 return mono_generic_virtual_remoting_trampoline;
1360 #endif
1361 case MONO_TRAMPOLINE_VCALL:
1362 return mono_vcall_trampoline;
1363 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1364 case MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD:
1365 return mono_handler_block_guard_trampoline;
1366 #endif
1367 default:
1368 g_assert_not_reached ();
1369 return NULL;
1373 static guchar*
1374 create_trampoline_code (MonoTrampolineType tramp_type)
1376 MonoTrampInfo *info;
1377 guchar *code;
1379 code = mono_arch_create_generic_trampoline (tramp_type, &info, FALSE);
1380 mono_tramp_info_register (info, NULL);
1382 return code;
1385 void
1386 mono_trampolines_init (void)
1388 mono_os_mutex_init_recursive (&trampolines_mutex);
1390 if (mono_aot_only)
1391 return;
1393 mono_trampoline_code [MONO_TRAMPOLINE_JIT] = create_trampoline_code (MONO_TRAMPOLINE_JIT);
1394 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
1395 mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
1396 #ifdef MONO_ARCH_AOT_SUPPORTED
1397 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = create_trampoline_code (MONO_TRAMPOLINE_AOT);
1398 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
1399 #endif
1400 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
1401 mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
1402 #ifndef DISABLE_REMOTING
1403 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
1404 #endif
1405 mono_trampoline_code [MONO_TRAMPOLINE_VCALL] = create_trampoline_code (MONO_TRAMPOLINE_VCALL);
1406 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1407 mono_trampoline_code [MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD] = create_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1408 mono_create_handler_block_trampoline ();
1409 #endif
1411 mono_counters_register ("Calls to trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &trampoline_calls);
1412 mono_counters_register ("JIT trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &jit_trampolines);
1413 mono_counters_register ("Unbox trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &unbox_trampolines);
1414 mono_counters_register ("Static rgctx trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &static_rgctx_trampolines);
1417 void
1418 mono_trampolines_cleanup (void)
1420 if (rgctx_lazy_fetch_trampoline_hash)
1421 g_hash_table_destroy (rgctx_lazy_fetch_trampoline_hash);
1422 if (rgctx_lazy_fetch_trampoline_hash_addr)
1423 g_hash_table_destroy (rgctx_lazy_fetch_trampoline_hash_addr);
1425 mono_os_mutex_destroy (&trampolines_mutex);
1428 guint8 *
1429 mono_get_trampoline_code (MonoTrampolineType tramp_type)
1431 g_assert (mono_trampoline_code [tramp_type]);
1433 return mono_trampoline_code [tramp_type];
1436 gpointer
1437 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1439 if (mono_aot_only)
1440 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
1441 else
1442 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
1445 gpointer
1446 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper, MonoError *error)
1448 MonoJitInfo *ji;
1449 gpointer code;
1450 guint32 code_size = 0;
1452 mono_error_init (error);
1454 code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1456 * We cannot recover the correct type of a shared generic
1457 * method from its native code address, so we use the
1458 * trampoline instead.
1459 * For synchronized methods, the trampoline adds the wrapper.
1461 if (code && !ji->has_generic_jit_info && !(method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
1462 return code;
1464 if (mono_llvm_only) {
1465 code = mono_jit_compile_method (method, error);
1466 if (!mono_error_ok (error))
1467 return NULL;
1468 return code;
1471 mono_domain_lock (domain);
1472 code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1473 mono_domain_unlock (domain);
1474 if (code)
1475 return code;
1477 code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1478 g_assert (code_size);
1480 ji = (MonoJitInfo *)mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO);
1481 ji->code_start = code;
1482 ji->code_size = code_size;
1483 ji->d.method = method;
1486 * mono_delegate_ctor needs to find the method metadata from the
1487 * trampoline address, so we save it here.
1490 mono_jit_info_table_add (domain, ji);
1492 mono_domain_lock (domain);
1493 g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1494 mono_domain_unlock (domain);
1496 return ji->code_start;
1499 static void
1500 method_not_found (void)
1502 g_assert_not_reached ();
1505 gpointer
1506 mono_create_jit_trampoline (MonoDomain *domain, MonoMethod *method, MonoError *error)
1508 gpointer tramp;
1510 mono_error_init (error);
1512 if (mono_aot_only) {
1513 if (mono_llvm_only && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1514 method = mono_marshal_get_synchronized_wrapper (method);
1516 /* Avoid creating trampolines if possible */
1517 gpointer code = mono_jit_find_compiled_method (domain, method);
1519 if (code)
1520 return code;
1521 if (mono_llvm_only) {
1522 if (method->wrapper_type == MONO_WRAPPER_PROXY_ISINST)
1523 /* These wrappers are not generated */
1524 return method_not_found;
1525 /* Methods are lazily initialized on first call, so this can't lead recursion */
1526 code = mono_jit_compile_method (method, error);
1527 if (!mono_error_ok (error))
1528 return NULL;
1529 return code;
1533 mono_domain_lock (domain);
1534 tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1535 mono_domain_unlock (domain);
1536 if (tramp)
1537 return tramp;
1539 tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1541 mono_domain_lock (domain);
1542 g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1543 mono_domain_unlock (domain);
1545 jit_trampolines++;
1547 return tramp;
1550 gpointer
1551 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1553 gpointer tramp;
1555 MonoDomain *domain = mono_domain_get ();
1556 guint8 *buf, *start;
1558 buf = start = (guint8 *)mono_domain_alloc0 (domain, 2 * sizeof (gpointer));
1560 *(gpointer*)(gpointer)buf = image;
1561 buf += sizeof (gpointer);
1562 *(guint32*)(gpointer)buf = token;
1564 tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1566 jit_trampolines++;
1568 return tramp;
1573 * mono_create_delegate_trampoline_info:
1575 * Create a trampoline info structure for the KLASS+METHOD pair.
1577 MonoDelegateTrampInfo*
1578 mono_create_delegate_trampoline_info (MonoDomain *domain, MonoClass *klass, MonoMethod *method)
1580 MonoMethod *invoke;
1581 MonoError error;
1582 MonoDelegateTrampInfo *tramp_info;
1583 MonoClassMethodPair pair, *dpair;
1584 guint32 code_size = 0;
1586 pair.klass = klass;
1587 pair.method = method;
1588 mono_domain_lock (domain);
1589 tramp_info = (MonoDelegateTrampInfo *)g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, &pair);
1590 mono_domain_unlock (domain);
1591 if (tramp_info)
1592 return tramp_info;
1594 invoke = mono_get_delegate_invoke (klass);
1595 g_assert (invoke);
1597 tramp_info = (MonoDelegateTrampInfo *)mono_domain_alloc0 (domain, sizeof (MonoDelegateTrampInfo));
1598 tramp_info->invoke = invoke;
1599 tramp_info->invoke_sig = mono_method_signature (invoke);
1600 tramp_info->impl_this = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1601 tramp_info->impl_nothis = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1602 tramp_info->method = method;
1603 if (method) {
1604 mono_error_init (&error);
1605 tramp_info->sig = mono_method_signature_checked (method, &error);
1606 tramp_info->need_rgctx_tramp = mono_method_needs_static_rgctx_invoke (method, FALSE);
1608 tramp_info->invoke_impl = mono_create_specific_trampoline (tramp_info, MONO_TRAMPOLINE_DELEGATE, domain, &code_size);
1609 g_assert (code_size);
1611 dpair = (MonoClassMethodPair *)mono_domain_alloc0 (domain, sizeof (MonoClassMethodPair));
1612 memcpy (dpair, &pair, sizeof (MonoClassMethodPair));
1614 /* store trampoline address */
1615 mono_domain_lock (domain);
1616 g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash, dpair, tramp_info);
1617 mono_domain_unlock (domain);
1619 return tramp_info;
1622 static void
1623 no_delegate_trampoline (void)
1625 g_assert_not_reached ();
1628 gpointer
1629 mono_create_delegate_trampoline (MonoDomain *domain, MonoClass *klass)
1631 if (mono_llvm_only)
1632 return no_delegate_trampoline;
1634 return mono_create_delegate_trampoline_info (domain, klass, NULL)->invoke_impl;
1637 gpointer
1638 mono_create_delegate_virtual_trampoline (MonoDomain *domain, MonoClass *klass, MonoMethod *method)
1640 MonoMethod *invoke = mono_get_delegate_invoke (klass);
1641 g_assert (invoke);
1643 return mono_get_delegate_virtual_invoke_impl (mono_method_signature (invoke), method);
1646 gpointer
1647 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1649 static gboolean inited = FALSE;
1650 static int num_trampolines = 0;
1651 MonoTrampInfo *info;
1653 gpointer tramp, ptr;
1655 mono_trampolines_lock ();
1656 if (rgctx_lazy_fetch_trampoline_hash)
1657 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1658 else
1659 tramp = NULL;
1660 mono_trampolines_unlock ();
1661 if (tramp)
1662 return tramp;
1664 if (mono_aot_only) {
1665 ptr = mono_aot_get_lazy_fetch_trampoline (offset);
1666 } else {
1667 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, FALSE);
1668 mono_tramp_info_register (info, NULL);
1669 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1672 mono_trampolines_lock ();
1673 if (!rgctx_lazy_fetch_trampoline_hash) {
1674 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1675 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1677 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1678 g_assert (offset != -1);
1679 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1680 mono_trampolines_unlock ();
1682 if (!inited) {
1683 mono_counters_register ("RGCTX num lazy fetch trampolines",
1684 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1685 inited = TRUE;
1687 num_trampolines++;
1689 return ptr;
1692 guint32
1693 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1695 int offset;
1697 mono_trampolines_lock ();
1698 if (rgctx_lazy_fetch_trampoline_hash_addr) {
1699 /* We store the real offset + 1 so we can detect when the lookup fails */
1700 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1701 if (offset)
1702 offset -= 1;
1703 else
1704 offset = -1;
1705 } else {
1706 offset = -1;
1708 mono_trampolines_unlock ();
1709 return offset;
1712 static const char*tramp_names [MONO_TRAMPOLINE_NUM] = {
1713 "jit",
1714 "jump",
1715 "rgctx_lazy_fetch",
1716 "aot",
1717 "aot_plt",
1718 "delegate",
1719 "restore_stack_prot",
1720 "generic_virtual_remoting",
1721 "vcall",
1722 "handler_block_guard"
1726 * mono_get_generic_trampoline_simple_name:
1729 const char*
1730 mono_get_generic_trampoline_simple_name (MonoTrampolineType tramp_type)
1732 return tramp_names [tramp_type];
1736 * mono_get_generic_trampoline_name:
1738 * Returns a pointer to malloc-ed memory.
1740 char*
1741 mono_get_generic_trampoline_name (MonoTrampolineType tramp_type)
1743 return g_strdup_printf ("generic_trampoline_%s", tramp_names [tramp_type]);
1747 * mono_get_rgctx_fetch_trampoline_name:
1749 * Returns a pointer to malloc-ed memory.
1751 char*
1752 mono_get_rgctx_fetch_trampoline_name (int slot)
1754 gboolean mrgctx;
1755 int index;
1757 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
1758 index = MONO_RGCTX_SLOT_INDEX (slot);
1760 return g_strdup_printf ("rgctx_fetch_trampoline_%s_%d", mrgctx ? "mrgctx" : "rgctx", index);
1764 * mini_get_single_step_trampoline:
1766 * Return a trampoline which calls debugger_agent_single_step_from_context ().
1768 gpointer
1769 mini_get_single_step_trampoline (void)
1771 static gpointer trampoline;
1773 if (!trampoline) {
1774 gpointer tramp;
1776 if (mono_aot_only) {
1777 tramp = mono_aot_get_trampoline ("sdb_single_step_trampoline");
1778 } else {
1779 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
1780 MonoTrampInfo *info;
1781 tramp = mono_arch_create_sdb_trampoline (TRUE, &info, FALSE);
1782 mono_tramp_info_register (info, NULL);
1783 #else
1784 tramp = NULL;
1785 g_assert_not_reached ();
1786 #endif
1788 mono_memory_barrier ();
1789 trampoline = tramp;
1792 return trampoline;
1796 * mini_get_breakpoint_trampoline:
1798 * Return a trampoline which calls debugger_agent_breakpoint_from_context ().
1800 gpointer
1801 mini_get_breakpoint_trampoline (void)
1803 static gpointer trampoline;
1805 if (!trampoline) {
1806 gpointer tramp;
1808 if (mono_aot_only) {
1809 tramp = mono_aot_get_trampoline ("sdb_breakpoint_trampoline");
1810 } else {
1811 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
1812 MonoTrampInfo *info;
1813 tramp = mono_arch_create_sdb_trampoline (FALSE, &info, FALSE);
1814 mono_tramp_info_register (info, NULL);
1815 #else
1816 tramp = NULL;
1817 g_assert_not_reached ();
1818 #endif
1820 mono_memory_barrier ();
1821 trampoline = tramp;
1824 return trampoline;