5 #include <mono/metadata/appdomain.h>
6 #include <mono/metadata/metadata-internals.h>
7 #include <mono/metadata/marshal.h>
8 #include <mono/metadata/tabledefs.h>
9 #include <mono/utils/mono-counters.h>
12 #include "debug-mini.h"
15 * Address of the trampoline code. This is used by the debugger to check
16 * whether a method is a trampoline.
18 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
20 static GHashTable *class_init_hash_addr = NULL;
21 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
22 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
24 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
25 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
26 static CRITICAL_SECTION trampolines_mutex;
29 get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr, gboolean need_rgctx_tramp)
34 * The unbox trampolines call the method directly, so need to add
35 * an rgctx tramp before them.
37 return mono_create_static_rgctx_trampoline (m, mono_aot_get_unbox_trampoline (m));
39 return mono_aot_get_unbox_trampoline (m);
41 return mono_arch_get_unbox_trampoline (gsctx, m, addr);
45 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
53 rgctx_tramp_info_equal (gconstpointer ka, gconstpointer kb)
55 const RgctxTrampInfo *i1 = ka;
56 const RgctxTrampInfo *i2 = kb;
58 if (i1->m == i2->m && i1->addr == i2->addr)
65 rgctx_tramp_info_hash (gconstpointer data)
67 const RgctxTrampInfo *info = data;
69 return GPOINTER_TO_UINT (info->m) ^ GPOINTER_TO_UINT (info->addr);
73 * mono_create_static_rgctx_trampoline:
75 * Return a static rgctx trampoline for M which branches to ADDR which should
76 * point to the compiled code of M.
78 * Static rgctx trampolines are used when a shared generic method which doesn't
79 * have a this argument is called indirectly, ie. from code which can't pass in
80 * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
81 * methods code. These trampolines are similar to the unbox trampolines, they
82 * perform the same task as the static rgctx wrappers, but they are smaller/faster,
83 * and can be made to work with full AOT.
86 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
91 RgctxTrampInfo tmp_info;
94 if (mini_method_get_context (m)->method_inst)
95 ctx = mono_method_lookup_rgctx (mono_class_vtable (mono_domain_get (), m->klass), mini_method_get_context (m)->method_inst);
97 ctx = mono_class_vtable (mono_domain_get (), m->klass);
99 domain = mono_domain_get ();
102 * In the AOT case, addr might point to either the method, or to an unbox trampoline,
103 * so make the hash keyed on the m+addr pair.
105 mono_domain_lock (domain);
106 if (!domain_jit_info (domain)->static_rgctx_trampoline_hash)
107 domain_jit_info (domain)->static_rgctx_trampoline_hash = g_hash_table_new (rgctx_tramp_info_hash, rgctx_tramp_info_equal);
109 tmp_info.addr = addr;
110 res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
112 mono_domain_unlock (domain);
117 res = mono_aot_get_static_rgctx_trampoline (ctx, addr);
119 res = mono_arch_get_static_rgctx_trampoline (m, ctx, addr);
121 mono_domain_lock (domain);
122 /* Duplicates inserted while we didn't hold the lock are OK */
123 info = mono_domain_alloc (domain, sizeof (RgctxTrampInfo));
126 g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, info, res);
127 mono_domain_unlock (domain);
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 ();
144 mono_get_vcall_slot_addr (guint8* code, mgreg_t *regs)
148 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
151 return (gpointer*)((char*)vt + displacement);
154 #ifdef MONO_ARCH_HAVE_IMT
157 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp)
159 MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (method), regs, code);
160 MonoVTable *vt = this_argument->vtable;
161 int displacement = slot - ((gpointer*)vt);
163 if (displacement > 0) {
164 /* slot is in the vtable, not in the IMT */
166 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
170 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
171 int interface_offset;
172 int imt_slot = MONO_IMT_SIZE + displacement;
174 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
176 if (interface_offset < 0) {
177 g_print ("%s doesn't implement interface %s\n", mono_type_get_name_full (&vt->klass->byval_arg, 0), mono_type_get_name_full (&imt_method->klass->byval_arg, 0));
178 g_assert_not_reached ();
180 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
185 if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
186 MonoGenericContext context = { NULL, NULL };
189 * Generic virtual method, imt_method contains the inflated interface
190 * method, need to get the inflated impl method.
192 /* imt_method->slot might not be set */
193 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
195 if (impl->klass->generic_class)
196 context.class_inst = impl->klass->generic_class->context.class_inst;
197 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
198 impl = mono_class_inflate_generic_method (impl, &context);
200 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + imt_method->slot);
203 if (mono_method_needs_static_rgctx_invoke (impl, FALSE))
204 *need_rgctx_tramp = TRUE;
208 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
209 method->klass->name_space, method->klass->name, method->name,
210 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
213 g_assert (imt_slot < MONO_IMT_SIZE);
214 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
215 int slot = mono_method_get_vtable_index (imt_method);
217 gpointer *vtable_slot;
219 g_assert (slot != -1);
220 vtable_offset = interface_offset + slot;
221 vtable_slot = & (vt->vtable [vtable_offset]);
223 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);
228 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
237 * common_call_trampoline:
239 * The code to handle normal, virtual, and interface method calls and jumps, both
240 * from JITted and LLVM compiled code.
243 common_call_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp, MonoVTable *vt, gpointer *vtable_slot, gboolean need_rgctx_tramp)
246 gboolean generic_shared = FALSE;
248 MonoMethod *declaring = NULL;
249 MonoMethod *generic_virtual = NULL;
251 gboolean proxy = FALSE;
252 gpointer *orig_vtable_slot;
256 orig_vtable_slot = vtable_slot;
258 if (m == MONO_FAKE_VTABLE_METHOD) {
264 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
266 printf ("Caller: %s\n", mono_method_full_name (ji->method, TRUE));
267 /* Print some debug info */
268 for (i = 0; i < 32; ++i)
269 printf ("0x%x ", code [-32 + i]);
273 displacement = (guint8*)vtable_slot - (guint8*)vt;
274 if (displacement > 0) {
275 int slot = (displacement - G_STRUCT_OFFSET (MonoVTable, vtable)) / sizeof (gpointer);
276 g_assert (slot >= 0);
278 /* Avoid loading metadata or creating a generic vtable if possible */
279 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, slot);
281 addr = mono_create_ftnptr (mono_domain_get (), addr);
282 if (addr && !vt->klass->valuetype) {
283 vtable_slot = mono_get_vcall_slot_addr (code, regs);
284 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
285 *vtable_slot = mono_get_addr_from_ftnptr (addr);
291 m = mono_class_get_vtable_entry (vt->klass, slot);
292 if (mono_method_needs_static_rgctx_invoke (m, FALSE))
293 need_rgctx_tramp = TRUE;
295 /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
297 /* We got here from an interface method: redirect to IMT handling */
298 m = MONO_FAKE_IMT_METHOD;
299 /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
303 /* this is the IMT trampoline */
304 #ifdef MONO_ARCH_HAVE_IMT
305 if (m == MONO_FAKE_IMT_METHOD) {
306 MonoMethod *impl_method;
307 MonoObject *this_arg;
309 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
310 * needs the signature to be able to find the this argument
312 m = mono_arch_find_imt_method (regs, code);
313 vtable_slot = orig_vtable_slot;
314 g_assert (vtable_slot);
316 this_arg = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
318 if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
319 /* Use the slow path for now */
321 m = mono_object_get_virtual_method (this_arg, m);
323 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp);
324 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
325 * to be called, so we compile it and go ahead as usual.
327 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
328 if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
329 /* Generic virtual method */
332 need_rgctx_tramp = TRUE;
341 MonoGenericContext context = { NULL, NULL };
342 MonoMethod *declaring;
345 declaring = mono_method_get_declaring_generic_method (m);
349 if (m->klass->generic_class)
350 context.class_inst = m->klass->generic_class->context.class_inst;
352 g_assert (!m->klass->generic_container);
354 #ifdef MONO_ARCH_HAVE_IMT
355 generic_virtual = mono_arch_find_imt_method (regs, code);
357 if (generic_virtual) {
358 g_assert (generic_virtual->is_inflated);
359 context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
362 m = mono_class_inflate_generic_method (declaring, &context);
363 /* FIXME: only do this if the method is sharable */
364 need_rgctx_tramp = TRUE;
365 } else if ((context_used = mono_method_check_context_used (m))) {
366 MonoClass *klass = NULL;
367 MonoMethod *actual_method = NULL;
368 MonoVTable *vt = NULL;
369 MonoGenericInst *method_inst = NULL;
372 generic_shared = TRUE;
376 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
377 #ifdef MONO_ARCH_RGCTX_REG
378 MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable (regs, code);
380 klass = mrgctx->class_vtable->klass;
381 method_inst = mrgctx->method_inst;
383 g_assert_not_reached ();
385 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
386 #ifdef MONO_ARCH_RGCTX_REG
387 MonoVTable *vtable = mono_arch_find_static_call_vtable (regs, code);
389 klass = vtable->klass;
391 g_assert_not_reached ();
394 #ifdef MONO_ARCH_HAVE_IMT
395 MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
397 vt = this_argument->vtable;
398 vtable_slot = orig_vtable_slot;
400 g_assert (this_argument->vtable->klass->inited);
401 //mono_class_init (this_argument->vtable->klass);
404 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
410 g_assert (vtable_slot || klass);
413 int displacement = vtable_slot - ((gpointer*)vt);
415 g_assert_not_reached ();
417 g_assert (displacement > 0);
419 actual_method = vt->klass->vtable [displacement];
423 MonoGenericContext context = { NULL, NULL };
426 declaring = mono_method_get_declaring_generic_method (m);
430 if (klass->generic_class)
431 context.class_inst = klass->generic_class->context.class_inst;
432 else if (klass->generic_container)
433 context.class_inst = klass->generic_container->context.class_inst;
434 context.method_inst = method_inst;
436 actual_method = mono_class_inflate_generic_method (declaring, &context);
438 actual_method = mono_class_get_method_generic (klass, m);
442 g_assert (actual_method);
443 g_assert (actual_method->klass == klass);
445 if (actual_method->is_inflated)
446 declaring = mono_method_get_declaring_generic_method (actual_method);
453 if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
457 ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
461 /* Avoid recursion */
462 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
463 m = mono_marshal_get_synchronized_wrapper (m);
466 /* Calls made through delegates on platforms without delegate trampolines */
467 if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
468 need_rgctx_tramp = TRUE;
470 addr = mono_compile_method (m);
473 mono_debugger_trampoline_compiled (code, m, addr);
475 if (need_rgctx_tramp)
476 addr = mono_create_static_rgctx_trampoline (m, addr);
478 if (generic_virtual) {
479 vtable_slot = orig_vtable_slot;
480 g_assert (vtable_slot);
482 if (vt->klass->valuetype)
483 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
485 mono_method_add_generic_virtual_invocation (mono_domain_get (),
487 generic_virtual, addr);
492 /* the method was jumped to */
494 MonoDomain *domain = mono_domain_get ();
496 /* Patch the got entries pointing to this method */
498 * We do this here instead of in mono_codegen () to cover the case when m
499 * was loaded from an aot image.
501 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
504 mono_domain_lock (domain);
505 list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
507 for (tmp = list; tmp; tmp = tmp->next) {
508 gpointer *got_slot = tmp->data;
511 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
514 mono_domain_unlock (domain);
520 vtable_slot = orig_vtable_slot;
523 if (m->klass->valuetype)
524 addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
525 g_assert (*vtable_slot);
527 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
528 #ifdef MONO_ARCH_HAVE_IMT
529 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, NULL, &need_rgctx_tramp);
531 *vtable_slot = mono_get_addr_from_ftnptr (addr);
535 guint8 *plt_entry = mono_aot_get_plt_entry (code);
538 mono_arch_patch_plt_entry (plt_entry, NULL, regs, addr);
539 } else if (!generic_shared || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
540 mono_domain_lookup_shared_generic (mono_domain_get (), declaring)) {
541 if (generic_shared) {
542 if (m->wrapper_type != MONO_WRAPPER_NONE)
543 m = mono_marshal_method_from_wrapper (m);
544 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
547 /* Patch calling code */
552 mono_jit_info_table_find (mono_domain_get (), (char*)code);
553 MonoJitInfo *target_ji =
554 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
556 if (mono_method_same_domain (ji, target_ji))
557 mono_arch_patch_callsite (ji->code_start, code, addr);
566 * mono_magic_trampoline:
568 * This trampoline handles calls from JITted code.
571 mono_magic_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp)
573 gpointer *vtable_slot;
577 if (code && !mono_use_llvm)
578 vt = mono_arch_get_vcall_slot (code, regs, &displacement);
582 vtable_slot = (gpointer*)((char*)vt + displacement);
586 return common_call_trampoline (regs, code, arg, tramp, vt, vtable_slot, FALSE);
589 #ifdef MONO_ARCH_LLVM_SUPPORTED
591 * mono_llvm_vcall_trampoline:
593 * This trampoline handles virtual calls when using LLVM.
596 mono_llvm_vcall_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
600 gpointer *vtable_slot;
604 * We have the method which is called, we need to obtain the vtable slot without
605 * disassembly which is impossible with LLVM.
606 * So we use the this argument.
608 this = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
611 slot = mono_method_get_vtable_slot (m);
613 g_assert (slot != -1);
615 g_assert (this->vtable->klass->vtable [slot] == m);
619 g_assert (!m->is_generic);
621 vtable_slot = &(vt->vtable [slot]);
623 return common_call_trampoline (regs, code, m, tramp, vt, vtable_slot, mono_method_needs_static_rgctx_invoke (m, 0));
628 mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
630 MonoGenericContext context = { NULL, NULL };
631 MonoMethod *imt_method, *declaring;
634 g_assert (m->is_generic);
637 declaring = mono_method_get_declaring_generic_method (m);
641 if (m->klass->generic_class)
642 context.class_inst = m->klass->generic_class->context.class_inst;
644 g_assert (!m->klass->generic_container);
646 #ifdef MONO_ARCH_HAVE_IMT
647 imt_method = mono_arch_find_imt_method (regs, code);
648 if (imt_method->is_inflated)
649 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
651 m = mono_class_inflate_generic_method (declaring, &context);
652 m = mono_marshal_get_remoting_invoke_with_check (m);
654 addr = mono_compile_method (m);
657 mono_debugger_trampoline_compiled (NULL, m, addr);
663 * mono_aot_trampoline:
665 * This trampoline handles calls made from AOT code. We try to bypass the
666 * normal JIT compilation logic to avoid loading the metadata for the method.
668 #ifdef MONO_ARCH_AOT_SUPPORTED
670 mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info,
675 MonoMethod *method = NULL;
677 gpointer *vtable_slot;
678 gboolean is_got_entry;
680 gboolean need_rgctx_tramp = FALSE;
682 image = *(gpointer*)(gpointer)token_info;
683 token_info += sizeof (gpointer);
684 token = *(guint32*)(gpointer)token_info;
686 addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
688 method = mono_get_method (image, token, NULL);
691 /* Use the generic code */
692 return mono_magic_trampoline (regs, code, method, tramp);
695 addr = mono_create_ftnptr (mono_domain_get (), addr);
697 vtable_slot = mono_get_vcall_slot_addr (code, regs);
698 g_assert (!vtable_slot);
700 /* This is a normal call through a PLT entry */
701 plt_entry = mono_aot_get_plt_entry (code);
702 g_assert (plt_entry);
704 mono_arch_patch_plt_entry (plt_entry, NULL, regs, addr);
706 is_got_entry = FALSE;
709 * Since AOT code is only used in the root domain,
710 * mono_domain_get () != mono_get_root_domain () means the calling method
711 * is AppDomain:InvokeInDomain, so this is the same check as in
712 * mono_method_same_domain () but without loading the metadata for the method.
714 if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
715 #ifdef MONO_ARCH_HAVE_IMT
717 method = mono_get_method (image, token, NULL);
718 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, method, NULL, &need_rgctx_tramp);
727 * mono_aot_plt_trampoline:
729 * This trampoline handles calls made from AOT code through the PLT table.
732 mono_aot_plt_trampoline (mgreg_t *regs, guint8 *code, guint8 *aot_module,
735 guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
737 return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
742 * mono_class_init_trampoline:
744 * This method calls mono_runtime_class_init () to run the static constructor
745 * for the type, then patches the caller code so it is not called again.
748 mono_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
750 guint8 *plt_entry = mono_aot_get_plt_entry (code);
752 mono_runtime_class_init (vtable);
755 mono_arch_nullify_plt_entry (plt_entry, regs);
757 mono_arch_nullify_class_init_trampoline (code, regs);
762 * mono_generic_class_init_trampoline:
764 * This method calls mono_runtime_class_init () to run the static constructor
768 mono_generic_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
770 mono_runtime_class_init (vtable);
774 mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, guint8 *tramp)
776 #ifdef MONO_ARCH_VTABLE_REG
777 static gboolean inited = FALSE;
778 static int num_lookups = 0;
779 guint32 slot = GPOINTER_TO_UINT (data);
780 mgreg_t *r = (mgreg_t*)regs;
781 gpointer arg = (gpointer)(gssize)r [MONO_ARCH_VTABLE_REG];
782 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
783 gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
786 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
793 return mono_method_fill_runtime_generic_context (arg, index);
795 return mono_class_fill_runtime_generic_context (arg, index);
797 g_assert_not_reached ();
802 mono_monitor_enter_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
804 mono_monitor_enter (obj);
808 mono_monitor_exit_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
810 mono_monitor_exit (obj);
813 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
816 * mono_delegate_trampoline:
818 * This trampoline handles calls made to Delegate:Invoke ().
819 * This is called once the first time a delegate is invoked, so it must be fast.
822 mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
824 MonoDomain *domain = mono_domain_get ();
825 MonoDelegate *delegate;
828 MonoMethod *method = NULL;
829 gboolean multicast, callvirt;
830 gboolean need_rgctx_tramp = FALSE;
831 MonoMethod *invoke = tramp_data [0];
832 guint8 *impl_this = tramp_data [1];
833 guint8 *impl_nothis = tramp_data [2];
835 /* Obtain the delegate object according to the calling convention */
838 * Avoid calling mono_get_generic_context_from_code () now since it is expensive,
839 * get_this_arg_from_call will call it if needed.
841 delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
843 if (delegate->method) {
844 method = delegate->method;
847 * delegate->method_ptr == NULL means the delegate was initialized by
848 * mini_delegate_ctor, while != NULL means it is initialized by
849 * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
850 * (ctor_with_method () does this, but it doesn't store the wrapper back into
853 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class) {
855 if (((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class != mono_defaults.com_object_class &&
856 !((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class->is_com_object)
858 method = mono_marshal_get_remoting_invoke (method);
860 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
861 method = mono_marshal_get_unbox_wrapper (method);
863 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
867 callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
869 if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
870 method = mono_marshal_get_synchronized_wrapper (method);
872 if (method && mono_method_needs_static_rgctx_invoke (method, FALSE))
873 need_rgctx_tramp = TRUE;
876 * If the called address is a trampoline, replace it with the compiled method so
877 * further calls don't have to go through the trampoline.
879 if (method && !callvirt) {
880 /* Avoid the overhead of looking up an already compiled method if possible */
881 if (delegate->method_code && *delegate->method_code) {
882 delegate->method_ptr = *delegate->method_code;
884 delegate->method_ptr = mono_compile_method (method);
885 if (need_rgctx_tramp)
886 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
887 if (delegate->method_code)
888 *delegate->method_code = delegate->method_ptr;
889 mono_debugger_trampoline_compiled (NULL, method, delegate->method_ptr);
892 if (need_rgctx_tramp)
893 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
896 multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
897 if (!multicast && !callvirt) {
898 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
899 /* Closed static delegate */
902 code = delegate->target ? impl_this : impl_nothis;
905 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
910 /* The general, unoptimized case */
911 m = mono_marshal_get_delegate_invoke (invoke, delegate);
912 code = mono_compile_method (m);
913 delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
914 mono_debugger_trampoline_compiled (NULL, m, delegate->invoke_impl);
922 * mono_get_trampoline_func:
924 * Return the C function which needs to be called by the generic trampoline of type
928 mono_get_trampoline_func (MonoTrampolineType tramp_type)
930 switch (tramp_type) {
931 case MONO_TRAMPOLINE_JIT:
932 case MONO_TRAMPOLINE_JUMP:
933 return mono_magic_trampoline;
934 case MONO_TRAMPOLINE_CLASS_INIT:
935 return mono_class_init_trampoline;
936 case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
937 return mono_generic_class_init_trampoline;
938 case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
939 return mono_rgctx_lazy_fetch_trampoline;
940 #ifdef MONO_ARCH_AOT_SUPPORTED
941 case MONO_TRAMPOLINE_AOT:
942 return mono_aot_trampoline;
943 case MONO_TRAMPOLINE_AOT_PLT:
944 return mono_aot_plt_trampoline;
946 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
947 case MONO_TRAMPOLINE_DELEGATE:
948 return mono_delegate_trampoline;
950 case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
951 return mono_altstack_restore_prot;
952 case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
953 return mono_generic_virtual_remoting_trampoline;
954 case MONO_TRAMPOLINE_MONITOR_ENTER:
955 return mono_monitor_enter_trampoline;
956 case MONO_TRAMPOLINE_MONITOR_EXIT:
957 return mono_monitor_exit_trampoline;
958 #ifdef MONO_ARCH_LLVM_SUPPORTED
959 case MONO_TRAMPOLINE_LLVM_VCALL:
960 return mono_llvm_vcall_trampoline;
963 g_assert_not_reached ();
969 mono_trampolines_init (void)
971 InitializeCriticalSection (&trampolines_mutex);
976 mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
977 mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
978 mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
979 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
980 mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
981 #ifdef MONO_ARCH_AOT_SUPPORTED
982 mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
983 mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
985 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
986 mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
988 mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
989 mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
990 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
991 mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
992 #ifdef MONO_ARCH_LLVM_SUPPORTED
993 mono_trampoline_code [MONO_TRAMPOLINE_LLVM_VCALL] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_LLVM_VCALL);
998 mono_trampolines_cleanup (void)
1000 if (class_init_hash_addr)
1001 g_hash_table_destroy (class_init_hash_addr);
1003 DeleteCriticalSection (&trampolines_mutex);
1007 mono_get_trampoline_code (MonoTrampolineType tramp_type)
1009 g_assert (mono_trampoline_code [tramp_type]);
1011 return mono_trampoline_code [tramp_type];
1015 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1018 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
1020 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
1024 mono_create_class_init_trampoline (MonoVTable *vtable)
1027 MonoDomain *domain = vtable->domain;
1029 g_assert (!vtable->klass->generic_container);
1031 /* previously created trampoline code */
1032 mono_domain_lock (domain);
1034 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
1036 mono_domain_unlock (domain);
1040 code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
1042 ptr = mono_create_ftnptr (domain, code);
1044 /* store trampoline address */
1045 mono_domain_lock (domain);
1046 g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
1048 mono_domain_unlock (domain);
1050 mono_trampolines_lock ();
1051 if (!class_init_hash_addr)
1052 class_init_hash_addr = g_hash_table_new (NULL, NULL);
1053 g_hash_table_insert (class_init_hash_addr, ptr, vtable);
1054 mono_trampolines_unlock ();
1060 mono_create_generic_class_init_trampoline (void)
1062 #ifdef MONO_ARCH_VTABLE_REG
1063 static gpointer code;
1065 mono_trampolines_lock ();
1069 code = mono_aot_get_named_code ("generic_class_init_trampoline");
1071 code = mono_arch_create_generic_class_init_trampoline ();
1074 mono_trampolines_unlock ();
1078 g_assert_not_reached ();
1083 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
1087 guint32 code_size = 0;
1089 code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1091 * We cannot recover the correct type of a shared generic
1092 * method from its native code address, so we use the
1093 * trampoline instead.
1095 if (code && !ji->has_generic_jit_info)
1098 mono_domain_lock (domain);
1099 code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1100 mono_domain_unlock (domain);
1104 code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1105 g_assert (code_size);
1107 ji = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO);
1108 ji->code_start = code;
1109 ji->code_size = code_size;
1110 ji->method = method;
1113 * mono_delegate_ctor needs to find the method metadata from the
1114 * trampoline address, so we save it here.
1117 mono_jit_info_table_add (domain, ji);
1119 mono_domain_lock (domain);
1120 g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1121 mono_domain_unlock (domain);
1123 return ji->code_start;
1127 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
1131 if (mono_aot_only) {
1132 /* Avoid creating trampolines if possible */
1133 gpointer code = mono_jit_find_compiled_method (domain, method);
1139 mono_domain_lock (domain);
1140 tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1141 mono_domain_unlock (domain);
1145 tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1147 mono_domain_lock (domain);
1148 g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1149 mono_domain_unlock (domain);
1151 mono_jit_stats.method_trampolines++;
1157 mono_create_jit_trampoline (MonoMethod *method)
1159 return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
1163 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1167 MonoDomain *domain = mono_domain_get ();
1168 guint8 *buf, *start;
1170 buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
1172 *(gpointer*)(gpointer)buf = image;
1173 buf += sizeof (gpointer);
1174 *(guint32*)(gpointer)buf = token;
1176 tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1178 mono_jit_stats.method_trampolines++;
1184 mono_create_delegate_trampoline (MonoClass *klass)
1186 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1187 MonoDomain *domain = mono_domain_get ();
1189 guint32 code_size = 0;
1190 gpointer *tramp_data;
1193 mono_domain_lock (domain);
1194 ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
1195 mono_domain_unlock (domain);
1199 // Precompute the delegate invoke impl and pass it to the delegate trampoline
1200 invoke = mono_get_delegate_invoke (klass);
1203 tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
1204 tramp_data [0] = invoke;
1205 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1206 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1208 ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
1209 g_assert (code_size);
1211 /* store trampoline address */
1212 mono_domain_lock (domain);
1213 g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
1215 mono_domain_unlock (domain);
1224 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1226 static gboolean inited = FALSE;
1227 static int num_trampolines = 0;
1229 gpointer tramp, ptr;
1232 return mono_aot_get_lazy_fetch_trampoline (offset);
1234 mono_trampolines_lock ();
1235 if (rgctx_lazy_fetch_trampoline_hash)
1236 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1239 mono_trampolines_unlock ();
1243 tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
1244 ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1246 mono_trampolines_lock ();
1247 if (!rgctx_lazy_fetch_trampoline_hash) {
1248 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1249 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1251 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1252 g_assert (offset != -1);
1253 g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1254 mono_trampolines_unlock ();
1257 mono_counters_register ("RGCTX num lazy fetch trampolines",
1258 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1267 mono_create_monitor_enter_trampoline (void)
1269 static gpointer code;
1271 if (mono_aot_only) {
1273 code = mono_aot_get_named_code ("monitor_enter_trampoline");
1277 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1278 mono_trampolines_lock ();
1281 code = mono_arch_create_monitor_enter_trampoline ();
1283 mono_trampolines_unlock ();
1286 g_assert_not_reached ();
1293 mono_create_monitor_exit_trampoline (void)
1295 static gpointer code;
1297 if (mono_aot_only) {
1299 code = mono_aot_get_named_code ("monitor_exit_trampoline");
1303 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1304 mono_trampolines_lock ();
1307 code = mono_arch_create_monitor_exit_trampoline ();
1309 mono_trampolines_unlock ();
1312 g_assert_not_reached ();
1317 #ifdef MONO_ARCH_LLVM_SUPPORTED
1319 * mono_create_llvm_vcall_trampoline:
1321 * LLVM emits code for virtual calls which mono_get_vcall_slot is unable to
1322 * decode, i.e. only the final branch address is available:
1323 * mov <offset>(%rax), %rax
1324 * <random code inserted by instruction scheduling>
1327 * To work around this problem, we don't use the common vtable trampoline when
1328 * llvm is enabled. Instead, we use one trampoline per method.
1331 mono_create_llvm_vcall_trampoline (MonoMethod *method)
1336 domain = mono_domain_get ();
1338 mono_domain_lock (domain);
1339 res = g_hash_table_lookup (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method);
1340 mono_domain_unlock (domain);
1344 res = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_LLVM_VCALL, domain, NULL);
1346 mono_domain_lock (domain);
1347 g_hash_table_insert (domain_jit_info (domain)->llvm_vcall_trampoline_hash, method, res);
1348 mono_domain_unlock (domain);
1354 * mono_create_llvm_imt_trampoline:
1356 * LLVM compiled code can't pass in the IMT argument, so we use this trampoline, which
1357 * sets the IMT argument, then branches to the contents of the vtable slot given by
1358 * vt_offset in the vtable which is obtained from the argument list.
1361 mono_create_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
1363 #ifdef MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE
1364 return mono_arch_get_llvm_imt_trampoline (domain, m, vt_offset);
1366 g_assert_not_reached ();
1373 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
1377 mono_trampolines_lock ();
1378 if (class_init_hash_addr)
1379 res = g_hash_table_lookup (class_init_hash_addr, addr);
1382 mono_trampolines_unlock ();
1387 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1391 mono_trampolines_lock ();
1392 if (rgctx_lazy_fetch_trampoline_hash_addr) {
1393 /* We store the real offset + 1 so we can detect when the lookup fails */
1394 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1402 mono_trampolines_unlock ();