0cda2932896de5c8cad373a4e0e8e45825c3a530
[mono.git] / mono / mini / mini-trampolines.c
1
2 #include <config.h>
3 #include <glib.h>
4
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>
10 #include <mono/utils/mono-error-internals.h>
11
12 #include "mini.h"
13 #include "debug-mini.h"
14
15 /*
16  * Address of the trampoline code.  This is used by the debugger to check
17  * whether a method is a trampoline.
18  */
19 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
20
21 static GHashTable *class_init_hash_addr = NULL;
22 static GHashTable *rgctx_lazy_fetch_trampoline_hash = NULL;
23 static GHashTable *rgctx_lazy_fetch_trampoline_hash_addr = NULL;
24 static guint32 trampoline_calls;
25
26 #define mono_trampolines_lock() EnterCriticalSection (&trampolines_mutex)
27 #define mono_trampolines_unlock() LeaveCriticalSection (&trampolines_mutex)
28 static CRITICAL_SECTION trampolines_mutex;
29
30 static gpointer
31 get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr, gboolean need_rgctx_tramp)
32 {
33         if (mono_aot_only) {
34                 if (need_rgctx_tramp)
35                         /* 
36                          * The unbox trampolines call the method directly, so need to add
37                          * an rgctx tramp before them.
38                          */
39                         return mono_create_static_rgctx_trampoline (m, mono_aot_get_unbox_trampoline (m));
40                 else
41                         return mono_aot_get_unbox_trampoline (m);
42         } else {
43                 return mono_arch_get_unbox_trampoline (gsctx, m, addr);
44         }
45 }
46
47 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
48
49 typedef struct {
50         MonoMethod *m;
51         gpointer addr;
52 } RgctxTrampInfo;
53
54 static gint
55 rgctx_tramp_info_equal (gconstpointer ka, gconstpointer kb)
56 {
57         const RgctxTrampInfo *i1 = ka;
58         const RgctxTrampInfo *i2 = kb;
59
60         if (i1->m == i2->m && i1->addr == i2->addr)
61                 return 1;
62         else
63                 return 0;
64 }
65
66 static guint
67 rgctx_tramp_info_hash (gconstpointer data)
68 {
69         const RgctxTrampInfo *info = data;
70
71         return GPOINTER_TO_UINT (info->m) ^ GPOINTER_TO_UINT (info->addr);
72 }
73
74 /*
75  * mono_create_static_rgctx_trampoline:
76  *
77  *   Return a static rgctx trampoline for M which branches to ADDR which should
78  * point to the compiled code of M.
79  *
80  *   Static rgctx trampolines are used when a shared generic method which doesn't
81  * have a this argument is called indirectly, ie. from code which can't pass in
82  * the rgctx argument. The trampoline sets the rgctx argument and jumps to the
83  * methods code. These trampolines are similar to the unbox trampolines, they
84  * perform the same task as the static rgctx wrappers, but they are smaller/faster,
85  * and can be made to work with full AOT.
86  * On PPC addr should be an ftnptr and the return value is an ftnptr too.
87  */
88 gpointer
89 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
90 {
91         gpointer ctx;
92         gpointer res;
93         MonoDomain *domain;
94         RgctxTrampInfo tmp_info;
95         RgctxTrampInfo *info;
96
97 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
98         g_assert (((gpointer*)addr) [2] == 0);
99 #endif
100
101         if (mini_method_get_context (m)->method_inst)
102                 ctx = mono_method_lookup_rgctx (mono_class_vtable (mono_domain_get (), m->klass), mini_method_get_context (m)->method_inst);
103         else
104                 ctx = mono_class_vtable (mono_domain_get (), m->klass);
105
106         domain = mono_domain_get ();
107
108         /* 
109          * In the AOT case, addr might point to either the method, or to an unbox trampoline,
110          * so make the hash keyed on the m+addr pair.
111          */
112         mono_domain_lock (domain);
113         if (!domain_jit_info (domain)->static_rgctx_trampoline_hash)
114                 domain_jit_info (domain)->static_rgctx_trampoline_hash = g_hash_table_new (rgctx_tramp_info_hash, rgctx_tramp_info_equal);
115         tmp_info.m = m;
116         tmp_info.addr = addr;
117         res = g_hash_table_lookup (domain_jit_info (domain)->static_rgctx_trampoline_hash,
118                                                            &tmp_info);
119         mono_domain_unlock (domain);
120         if (res)
121                 return res;
122
123         if (mono_aot_only)
124                 res = mono_aot_get_static_rgctx_trampoline (ctx, addr);
125         else
126                 res = mono_arch_get_static_rgctx_trampoline (m, ctx, addr);
127
128         mono_domain_lock (domain);
129         /* Duplicates inserted while we didn't hold the lock are OK */
130         info = mono_domain_alloc (domain, sizeof (RgctxTrampInfo));
131         info->m = m;
132         info->addr = addr;
133         g_hash_table_insert (domain_jit_info (domain)->static_rgctx_trampoline_hash, info, res);
134         mono_domain_unlock (domain);
135
136         return res;
137 }
138 #else
139 gpointer
140 mono_create_static_rgctx_trampoline (MonoMethod *m, gpointer addr)
141 {
142         /* 
143          * This shouldn't happen as all arches which support generic sharing support
144          * static rgctx trampolines as well.
145          */
146         g_assert_not_reached ();
147 }
148 #endif
149
150 #ifdef MONO_ARCH_HAVE_IMT
151
152 static gpointer*
153 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, mgreg_t *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method, gboolean *need_rgctx_tramp, gboolean *variance_used)
154 {
155         MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (method), regs, code);
156         MonoVTable *vt = this_argument->vtable;
157         int displacement = slot - ((gpointer*)vt);
158
159         if (displacement > 0) {
160                 /* slot is in the vtable, not in the IMT */
161 #if DEBUG_IMT
162                 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
163 #endif
164                 return slot;
165         } else {
166                 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
167                 int interface_offset;
168                 int imt_slot = MONO_IMT_SIZE + displacement;
169
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
173                 if (interface_offset < 0) {
174                         g_error ("%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));
175                 }
176                 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
177
178                 if (impl_method) {
179                         MonoMethod *impl;
180
181                         if (imt_method->is_inflated && ((MonoMethodInflated*)imt_method)->context.method_inst) {
182                                 MonoGenericContext context = { NULL, NULL };
183
184                                 /* 
185                                  * Generic virtual method, imt_method contains the inflated interface 
186                                  * method, need to get the inflated impl method.
187                                  */
188                                 /* imt_method->slot might not be set */
189                                 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_declaring_generic_method (imt_method)->slot);
190
191                                 if (impl->klass->generic_class)
192                                         context.class_inst = impl->klass->generic_class->context.class_inst;
193                                 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
194                                 impl = mono_class_inflate_generic_method (impl, &context);
195                         } else {
196                                 impl = mono_class_get_vtable_entry (vt->klass, interface_offset + mono_method_get_vtable_slot (imt_method));
197                         }
198
199                         if (mono_method_needs_static_rgctx_invoke (impl, FALSE))
200                                 *need_rgctx_tramp = TRUE;
201
202                         *impl_method = impl;
203 #if DEBUG_IMT
204                 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
205                                 method->klass->name_space, method->klass->name, method->name, 
206                                 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
207 #endif
208                 }
209                 g_assert (imt_slot < MONO_IMT_SIZE);
210                 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
211                         int slot = mono_method_get_vtable_index (imt_method);
212                         int vtable_offset;
213                         gpointer *vtable_slot;
214
215                         g_assert (slot != -1);
216                         vtable_offset = interface_offset + slot;
217                         vtable_slot = & (vt->vtable [vtable_offset]);
218 #if DEBUG_IMT
219                         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);
220 #endif
221                         return vtable_slot;
222                 } else {
223 #if DEBUG_IMT
224                         printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
225 #endif
226                         return slot;
227                 }
228         }
229 }
230 #endif
231
232 /*
233  * This is a super-ugly hack to fix bug #616463.
234  *
235  * The problem is that we don't always set is_generic for generic
236  * method definitions.  See the comment at the end of
237  * mono_class_inflate_generic_method_full_checked() in class.c.
238  */
239 static gboolean
240 is_generic_method_definition (MonoMethod *m)
241 {
242         MonoGenericContext *context;
243         if (m->is_generic)
244                 return TRUE;
245         if (!m->is_inflated)
246                 return FALSE;
247
248         context = mono_method_get_context (m);
249         if (!context->method_inst)
250                 return FALSE;
251         if (context->method_inst == mono_method_get_generic_container (((MonoMethodInflated*)m)->declaring)->context.method_inst)
252                 return TRUE;
253         return FALSE;
254 }
255
256 /**
257  * common_call_trampoline:
258  *
259  *   The code to handle normal, virtual, and interface method calls and jumps, both
260  * from JITted and LLVM compiled code.
261  */
262 static gpointer
263 common_call_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8* tramp, MonoVTable *vt, gpointer *vtable_slot, gboolean need_rgctx_tramp)
264 {
265         gpointer addr, compiled_method;
266         gboolean generic_shared = FALSE;
267         MonoMethod *declaring = NULL;
268         MonoMethod *generic_virtual = NULL, *variant_iface = NULL;
269         int context_used;
270         gboolean virtual, proxy = FALSE, variance_used = FALSE;
271         gpointer *orig_vtable_slot, *imt_vcall_slot = NULL;
272         MonoJitInfo *ji = NULL;
273
274         virtual = (gpointer)vtable_slot > (gpointer)vt;
275
276         orig_vtable_slot = vtable_slot;
277
278 #ifdef MONO_ARCH_HAVE_IMT
279         /* IMT call */
280         if (vt && (gpointer)vtable_slot < (gpointer)vt) {
281                 MonoMethod *impl_method = NULL;
282                 MonoObject *this_arg;
283
284                 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
285                  * needs the signature to be able to find the this argument
286                  */
287                 m = mono_arch_find_imt_method (regs, code);
288                 vtable_slot = orig_vtable_slot;
289                 g_assert (vtable_slot);
290
291                 this_arg = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
292
293                 if (this_arg->vtable->klass == mono_defaults.transparent_proxy_class) {
294                         /* Use the slow path for now */
295                         proxy = TRUE;
296                     m = mono_object_get_virtual_method (this_arg, m);
297                 } else {
298                         imt_vcall_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, regs, code, m, &impl_method, &need_rgctx_tramp, &variance_used);
299                         vtable_slot = imt_vcall_slot;
300                         /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
301                          * to be called, so we compile it and go ahead as usual.
302                          */
303                         /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
304                         if (m->is_inflated && ((MonoMethodInflated*)m)->context.method_inst) {
305                                 /* Generic virtual method */
306                                 generic_virtual = m;
307                                 need_rgctx_tramp = TRUE;
308                         } else if (variance_used && mono_class_has_variant_generic_params (m->klass)) {
309                                 variant_iface = m;
310                         }
311                         m = impl_method;
312                 }
313         }
314 #endif
315
316         /*
317          * The virtual check is needed because is_generic_method_definition (m) could
318          * return TRUE for methods used in IMT calls too.
319          */
320         if (virtual && is_generic_method_definition (m)) {
321                 MonoGenericContext context = { NULL, NULL };
322                 MonoMethod *declaring;
323
324                 if (m->is_inflated)
325                         declaring = mono_method_get_declaring_generic_method (m);
326                 else
327                         declaring = m;
328
329                 if (m->klass->generic_class)
330                         context.class_inst = m->klass->generic_class->context.class_inst;
331                 else
332                         g_assert (!m->klass->generic_container);
333
334 #ifdef MONO_ARCH_HAVE_IMT
335                 generic_virtual = mono_arch_find_imt_method (regs, code);
336 #endif
337                 if (generic_virtual) {
338                         g_assert (generic_virtual->is_inflated);
339                         context.method_inst = ((MonoMethodInflated*)generic_virtual)->context.method_inst;
340                 }
341
342                 m = mono_class_inflate_generic_method (declaring, &context);
343                 /* FIXME: only do this if the method is sharable */
344                 need_rgctx_tramp = TRUE;
345         } else if ((context_used = mono_method_check_context_used (m))) {
346                 MonoClass *klass = NULL;
347                 MonoMethod *actual_method = NULL;
348                 MonoVTable *vt = NULL;
349                 MonoGenericInst *method_inst = NULL;
350
351                 vtable_slot = NULL;
352                 generic_shared = TRUE;
353
354                 g_assert (code);
355
356                 if (m->is_inflated && mono_method_get_context (m)->method_inst) {
357 #ifdef MONO_ARCH_RGCTX_REG
358                         MonoMethodRuntimeGenericContext *mrgctx = (MonoMethodRuntimeGenericContext*)mono_arch_find_static_call_vtable (regs, code);
359
360                         klass = mrgctx->class_vtable->klass;
361                         method_inst = mrgctx->method_inst;
362 #else
363                         g_assert_not_reached ();
364 #endif
365                 } else if ((m->flags & METHOD_ATTRIBUTE_STATIC) || m->klass->valuetype) {
366 #ifdef MONO_ARCH_RGCTX_REG
367                         MonoVTable *vtable = mono_arch_find_static_call_vtable (regs, code);
368
369                         klass = vtable->klass;
370 #else
371                         g_assert_not_reached ();
372 #endif
373                 } else {
374 #ifdef MONO_ARCH_HAVE_IMT
375                         MonoObject *this_argument = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (m), regs, code);
376
377                         vt = this_argument->vtable;
378                         vtable_slot = orig_vtable_slot;
379
380                         g_assert (this_argument->vtable->klass->inited);
381                         //mono_class_init (this_argument->vtable->klass);
382
383                         if (!vtable_slot)
384                                 klass = this_argument->vtable->klass->supertypes [m->klass->idepth - 1];
385 #else
386                         NOT_IMPLEMENTED;
387 #endif
388                 }
389
390                 g_assert (vtable_slot || klass);
391
392                 if (vtable_slot) {
393                         int displacement = vtable_slot - ((gpointer*)vt);
394
395                         g_assert_not_reached ();
396
397                         g_assert (displacement > 0);
398
399                         actual_method = vt->klass->vtable [displacement];
400                 }
401
402                 if (method_inst) {
403                         MonoGenericContext context = { NULL, NULL };
404
405                         if (m->is_inflated)
406                                 declaring = mono_method_get_declaring_generic_method (m);
407                         else
408                                 declaring = m;
409
410                         if (klass->generic_class)
411                                 context.class_inst = klass->generic_class->context.class_inst;
412                         else if (klass->generic_container)
413                                 context.class_inst = klass->generic_container->context.class_inst;
414                         context.method_inst = method_inst;
415
416                         actual_method = mono_class_inflate_generic_method (declaring, &context);
417                 } else {
418                         actual_method = mono_class_get_method_generic (klass, m);
419                 }
420
421                 g_assert (klass);
422                 g_assert (actual_method);
423                 g_assert (actual_method->klass == klass);
424
425                 if (actual_method->is_inflated)
426                         declaring = mono_method_get_declaring_generic_method (actual_method);
427                 else
428                         declaring = NULL;
429
430                 m = actual_method;
431         }
432
433         if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
434                 MonoJitInfo *ji;
435
436                 if (code)
437                         ji = mini_jit_info_table_find (mono_domain_get (), (char*)code, NULL);
438                 else
439                         ji = NULL;
440
441                 /* Avoid recursion */
442                 if (!(ji && ji->method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED))
443                         m = mono_marshal_get_synchronized_wrapper (m);
444         }
445
446         /* Calls made through delegates on platforms without delegate trampolines */
447         if (!code && mono_method_needs_static_rgctx_invoke (m, FALSE))
448                 need_rgctx_tramp = TRUE;
449
450         addr = compiled_method = mono_compile_method (m);
451         g_assert (addr);
452
453         mono_debugger_trampoline_compiled (code, m, addr);
454
455         if (need_rgctx_tramp)
456                 addr = mono_create_static_rgctx_trampoline (m, addr);
457
458         if (generic_virtual || variant_iface) {
459                 MonoMethod *target = generic_virtual ? generic_virtual : variant_iface;
460
461                 vtable_slot = orig_vtable_slot;
462                 g_assert (vtable_slot);
463
464                 if (vt->klass->valuetype) /*FIXME is this required variant iface?*/
465                         addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
466
467                 mono_method_add_generic_virtual_invocation (mono_domain_get (), 
468                                                                                                         vt, vtable_slot,
469                                                                                                         target, addr);
470
471                 return addr;
472         }
473
474         /* the method was jumped to */
475         if (!code) {
476                 MonoDomain *domain = mono_domain_get ();
477
478                 /* Patch the got entries pointing to this method */
479                 /* 
480                  * We do this here instead of in mono_codegen () to cover the case when m
481                  * was loaded from an aot image.
482                  */
483                 if (domain_jit_info (domain)->jump_target_got_slot_hash) {
484                         GSList *list, *tmp;
485
486                         mono_domain_lock (domain);
487                         list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_got_slot_hash, m);
488                         if (list) {
489                                 for (tmp = list; tmp; tmp = tmp->next) {
490                                         gpointer *got_slot = tmp->data;
491                                         *got_slot = addr;
492                                 }
493                                 g_hash_table_remove (domain_jit_info (domain)->jump_target_got_slot_hash, m);
494                                 g_slist_free (list);
495                         }
496                         mono_domain_unlock (domain);
497                 }
498
499                 return addr;
500         }
501
502         vtable_slot = orig_vtable_slot;
503
504         if (vtable_slot) {
505                 if (m->klass->valuetype)
506                         addr = get_unbox_trampoline (mono_get_generic_context_from_code (code), m, addr, need_rgctx_tramp);
507                 g_assert (*vtable_slot);
508
509                 if (!proxy && (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))) {
510                         if (imt_vcall_slot)
511                                 /* This is the vcall slot which gets called through the IMT thunk */
512                                 vtable_slot = imt_vcall_slot;
513                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
514                 }
515         }
516         else {
517                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
518
519                 if (plt_entry) {
520                                 mono_aot_patch_plt_entry (plt_entry, NULL, regs, addr);
521                 } else {
522                         if (generic_shared) {
523                                 if (m->wrapper_type != MONO_WRAPPER_NONE)
524                                         m = mono_marshal_method_from_wrapper (m);
525                                 g_assert (mono_method_is_generic_sharable_impl (m, FALSE));
526                         }
527
528                         /* Patch calling code */
529                         {
530                                 MonoJitInfo *target_ji = 
531                                         mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (compiled_method));
532
533                                 if (!ji)
534                                         ji = mono_jit_info_table_find (mono_domain_get (), (char*)code);
535
536                                 if (mono_method_same_domain (ji, target_ji))
537                                         mono_arch_patch_callsite (ji->code_start, code, addr);
538                         }
539                 }
540         }
541
542         return addr;
543 }
544
545 /**
546  * mono_magic_trampoline:
547  *
548  *   This trampoline handles normal calls from JITted code.
549  */
550 gpointer
551 mono_magic_trampoline (mgreg_t *regs, guint8 *code, gpointer arg, guint8* tramp)
552 {
553         trampoline_calls ++;
554
555         return common_call_trampoline (regs, code, arg, tramp, NULL, NULL, FALSE);
556 }
557
558 /*
559  * mono_vcall_trampoline:
560  *
561  *   This trampoline handles virtual calls.
562  */
563 static gpointer
564 mono_vcall_trampoline (mgreg_t *regs, guint8 *code, int slot, guint8 *tramp)
565 {
566         MonoObject *this;
567         MonoVTable *vt;
568         gpointer *vtable_slot;
569         MonoMethod *m;
570         gboolean need_rgctx_tramp = FALSE;
571         gpointer addr;
572
573         trampoline_calls ++;
574
575         /*
576          * We need to obtain the following pieces of information:
577          * - the method which needs to be compiled.
578          * - the vtable slot.
579          * We use one vtable trampoline per vtable slot index, so we need only the vtable,
580          * the other two can be computed from the vtable + the slot index.
581          */
582 #ifndef MONO_ARCH_THIS_AS_FIRST_ARG
583         /* All architectures should support this */
584         g_assert_not_reached ();
585 #endif
586
587         /*
588          * Obtain the vtable from the 'this' arg.
589          */
590         this = mono_arch_get_this_arg_from_call (NULL, NULL, regs, code);
591         g_assert (this);
592
593         vt = this->vtable;
594
595         if (slot >= 0) {
596                 /* Normal virtual call */
597                 vtable_slot = &(vt->vtable [slot]);
598
599                 /* Avoid loading metadata or creating a generic vtable if possible */
600                 addr = mono_aot_get_method_from_vt_slot (mono_domain_get (), vt, slot);
601                 if (addr && !vt->klass->valuetype) {
602                         if (mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
603                                 *vtable_slot = addr;
604
605                         return mono_create_ftnptr (mono_domain_get (), addr);
606                 }
607
608                 /*
609                  * Bug #616463 (see
610                  * is_generic_method_definition() above) also
611                  * goes away if we do a
612                  * mono_class_setup_vtable (vt->klass) here,
613                  * because we then inflate the method
614                  * correctly, put it in the cache, and the
615                  * "wrong" inflation invocation still looks up
616                  * the correctly inflated method.
617                  *
618                  * The hack above seems more stable and
619                  * trustworthy.
620                  */
621                 m = mono_class_get_vtable_entry (vt->klass, slot);
622
623                 need_rgctx_tramp = mono_method_needs_static_rgctx_invoke (m, 0);
624         } else {
625                 /* IMT call */
626                 vtable_slot = &(((gpointer*)vt) [slot]);
627
628                 m = NULL;
629                 need_rgctx_tramp = FALSE;
630         }
631
632         return common_call_trampoline (regs, code, m, tramp, vt, vtable_slot, need_rgctx_tramp);
633 }
634
635 gpointer
636 mono_generic_virtual_remoting_trampoline (mgreg_t *regs, guint8 *code, MonoMethod *m, guint8 *tramp)
637 {
638         MonoGenericContext context = { NULL, NULL };
639         MonoMethod *imt_method, *declaring;
640         gpointer addr;
641
642         trampoline_calls ++;
643
644         g_assert (m->is_generic);
645
646         if (m->is_inflated)
647                 declaring = mono_method_get_declaring_generic_method (m);
648         else
649                 declaring = m;
650
651         if (m->klass->generic_class)
652                 context.class_inst = m->klass->generic_class->context.class_inst;
653         else
654                 g_assert (!m->klass->generic_container);
655
656 #ifdef MONO_ARCH_HAVE_IMT
657         imt_method = mono_arch_find_imt_method (regs, code);
658         if (imt_method->is_inflated)
659                 context.method_inst = ((MonoMethodInflated*)imt_method)->context.method_inst;
660 #endif
661         m = mono_class_inflate_generic_method (declaring, &context);
662         m = mono_marshal_get_remoting_invoke_with_check (m);
663
664         addr = mono_compile_method (m);
665         g_assert (addr);
666
667         mono_debugger_trampoline_compiled (NULL, m, addr);
668
669         return addr;
670 }
671
672 /*
673  * mono_aot_trampoline:
674  *
675  *   This trampoline handles calls made from AOT code. We try to bypass the 
676  * normal JIT compilation logic to avoid loading the metadata for the method.
677  */
678 #ifdef MONO_ARCH_AOT_SUPPORTED
679 gpointer
680 mono_aot_trampoline (mgreg_t *regs, guint8 *code, guint8 *token_info, 
681                                          guint8* tramp)
682 {
683         MonoImage *image;
684         guint32 token;
685         MonoMethod *method = NULL;
686         gpointer addr;
687         guint8 *plt_entry;
688
689         trampoline_calls ++;
690
691         image = *(gpointer*)(gpointer)token_info;
692         token_info += sizeof (gpointer);
693         token = *(guint32*)(gpointer)token_info;
694
695         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
696         if (!addr) {
697                 method = mono_get_method (image, token, NULL);
698                 g_assert (method);
699
700                 /* Use the generic code */
701                 return mono_magic_trampoline (regs, code, method, tramp);
702         }
703
704         addr = mono_create_ftnptr (mono_domain_get (), addr);
705
706         /* This is a normal call through a PLT entry */
707         plt_entry = mono_aot_get_plt_entry (code);
708         g_assert (plt_entry);
709
710         mono_aot_patch_plt_entry (plt_entry, NULL, regs, addr);
711
712         return addr;
713 }
714
715 /*
716  * mono_aot_plt_trampoline:
717  *
718  *   This trampoline handles calls made from AOT code through the PLT table.
719  */
720 gpointer
721 mono_aot_plt_trampoline (mgreg_t *regs, guint8 *code, guint8 *aot_module, 
722                                                  guint8* tramp)
723 {
724         guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
725         gpointer res;
726
727         trampoline_calls ++;
728
729         res = mono_aot_plt_resolve (aot_module, plt_info_offset, code);
730         if (!res) {
731                 if (mono_loader_get_last_error ())
732                         mono_raise_exception (mono_loader_error_prepare_exception (mono_loader_get_last_error ()));
733                 // FIXME: Error handling (how ?)
734                 g_assert (res);
735         }
736
737         return res;
738 }
739 #endif
740
741 /**
742  * mono_class_init_trampoline:
743  *
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.
746  */
747 void
748 mono_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
749 {
750         guint8 *plt_entry = mono_aot_get_plt_entry (code);
751
752         trampoline_calls ++;
753
754         mono_runtime_class_init (vtable);
755
756         if (plt_entry) {
757                 mono_arch_nullify_plt_entry (plt_entry, regs);
758         } else {
759                 mono_arch_nullify_class_init_trampoline (code, regs);
760         }
761 }
762
763 /**
764  * mono_generic_class_init_trampoline:
765  *
766  * This method calls mono_runtime_class_init () to run the static constructor
767  * for the type.
768  */
769 void
770 mono_generic_class_init_trampoline (mgreg_t *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
771 {
772         trampoline_calls ++;
773
774         mono_runtime_class_init (vtable);
775 }
776
777 static gpointer
778 mono_rgctx_lazy_fetch_trampoline (mgreg_t *regs, guint8 *code, gpointer data, guint8 *tramp)
779 {
780 #ifdef MONO_ARCH_VTABLE_REG
781         static gboolean inited = FALSE;
782         static int num_lookups = 0;
783         guint32 slot = GPOINTER_TO_UINT (data);
784         mgreg_t *r = (mgreg_t*)regs;
785         gpointer arg = (gpointer)(gssize)r [MONO_ARCH_VTABLE_REG];
786         guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
787         gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
788
789         trampoline_calls ++;
790
791         if (!inited) {
792                 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
793                 inited = TRUE;
794         }
795
796         num_lookups++;
797
798         if (mrgctx)
799                 return mono_method_fill_runtime_generic_context (arg, index);
800         else
801                 return mono_class_fill_runtime_generic_context (arg, index);
802 #else
803         g_assert_not_reached ();
804 #endif
805 }
806
807 void
808 mono_monitor_enter_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
809 {
810         mono_monitor_enter (obj);
811 }
812
813 void
814 mono_monitor_exit_trampoline (mgreg_t *regs, guint8 *code, MonoObject *obj, guint8 *tramp)
815 {
816         mono_monitor_exit (obj);
817 }
818
819 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
820
821 /**
822  * mono_delegate_trampoline:
823  *
824  *   This trampoline handles calls made to Delegate:Invoke ().
825  * This is called once the first time a delegate is invoked, so it must be fast.
826  */
827 gpointer
828 mono_delegate_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
829 {
830         MonoDomain *domain = mono_domain_get ();
831         MonoDelegate *delegate;
832         MonoJitInfo *ji;
833         MonoMethod *m;
834         MonoMethod *method = NULL;
835         gboolean multicast, callvirt = FALSE;
836         gboolean need_rgctx_tramp = FALSE;
837         MonoMethod *invoke = tramp_data [0];
838         guint8 *impl_this = tramp_data [1];
839         guint8 *impl_nothis = tramp_data [2];
840         MonoError err;
841         MonoMethodSignature *sig;
842
843         trampoline_calls ++;
844
845         /* Obtain the delegate object according to the calling convention */
846
847         /* 
848          * Avoid calling mono_get_generic_context_from_code () now since it is expensive, 
849          * get_this_arg_from_call will call it if needed.
850          */
851         delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
852
853         if (delegate->method) {
854                 method = delegate->method;
855
856                 /*
857                  * delegate->method_ptr == NULL means the delegate was initialized by 
858                  * mini_delegate_ctor, while != NULL means it is initialized by 
859                  * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
860                  * (ctor_with_method () does this, but it doesn't store the wrapper back into
861                  * delegate->method).
862                  */
863                 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class) {
864 #ifndef DISABLE_COM
865                         if (((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class != mono_defaults.com_object_class && 
866                            !((MonoTransparentProxy *)delegate->target)->remote_class->proxy_class->is_com_object)
867 #endif
868                                 method = mono_marshal_get_remoting_invoke (method);
869                 }
870                 else {
871                         mono_error_init (&err);
872                         sig = mono_method_signature_checked (method, &err);
873                         if (!sig)
874                                 mono_error_raise_exception (&err);
875                                 
876                         if (sig->hasthis && method->klass->valuetype)
877                                 method = mono_marshal_get_unbox_wrapper (method);
878                 }
879         } else {
880                 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
881                 if (ji)
882                         method = ji->method;
883         }
884
885         if (method) {
886                 mono_error_init (&err);
887                 sig = mono_method_signature_checked (method, &err);
888                 if (!sig)
889                         mono_error_raise_exception (&err);
890
891                 callvirt = !delegate->target && sig->hasthis;
892         }
893
894         if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
895                 method = mono_marshal_get_synchronized_wrapper (method);
896
897         if (method && mono_method_needs_static_rgctx_invoke (method, FALSE))
898                 need_rgctx_tramp = TRUE;
899
900         /* 
901          * If the called address is a trampoline, replace it with the compiled method so
902          * further calls don't have to go through the trampoline.
903          */
904         if (method && !callvirt) {
905                 /* Avoid the overhead of looking up an already compiled method if possible */
906                 if (delegate->method_code && *delegate->method_code) {
907                         delegate->method_ptr = *delegate->method_code;
908                 } else {
909                         delegate->method_ptr = mono_compile_method (method);
910                         if (need_rgctx_tramp)
911                                 delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
912                         if (delegate->method_code)
913                                 *delegate->method_code = delegate->method_ptr;
914                         mono_debugger_trampoline_compiled (NULL, method, delegate->method_ptr);
915                 }
916         } else {
917                 if (need_rgctx_tramp)
918                         delegate->method_ptr = mono_create_static_rgctx_trampoline (method, delegate->method_ptr);
919         }
920
921         multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
922         if (!multicast && !callvirt) {
923                 if (method && (method->flags & METHOD_ATTRIBUTE_STATIC) && mono_method_signature (method)->param_count == mono_method_signature (invoke)->param_count + 1)
924                         /* Closed static delegate */
925                         code = impl_this;
926                 else
927                         code = delegate->target ? impl_this : impl_nothis;
928
929                 if (code) {
930                         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
931                         return code;
932                 }
933         }
934
935         /* The general, unoptimized case */
936         m = mono_marshal_get_delegate_invoke (invoke, delegate);
937         code = mono_compile_method (m);
938         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
939         mono_debugger_trampoline_compiled (NULL, m, delegate->invoke_impl);
940
941         return code;
942 }
943
944 #endif
945
946 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
947 static gpointer
948 mono_handler_block_guard_trampoline (mgreg_t *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
949 {
950         MonoContext ctx;
951         MonoException *exc;
952         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
953         gpointer resume_ip = jit_tls->handler_block_return_address;
954
955         memcpy (&ctx, &jit_tls->ex_ctx, sizeof (MonoContext));
956         MONO_CONTEXT_SET_IP (&ctx, jit_tls->handler_block_return_address);
957
958         jit_tls->handler_block_return_address = NULL;
959         jit_tls->handler_block = NULL;
960
961         if (!resume_ip) /*this should not happen, but we should avoid crashing */
962                 exc = mono_get_exception_execution_engine ("Invalid internal state, resuming abort after handler block but no resume ip found");
963         else
964                 exc = mono_thread_resume_interruption ();
965
966         if (exc) {
967                 static void (*restore_context) (MonoContext *);
968
969                 if (!restore_context)
970                         restore_context = mono_get_restore_context ();
971
972                 mono_handle_exception (&ctx, exc, NULL, FALSE);
973                 restore_context (&ctx);
974         }
975
976         return resume_ip;
977 }
978
979 gpointer
980 mono_create_handler_block_trampoline (void)
981 {
982         static gpointer code;
983
984         if (mono_aot_only) {
985                 g_assert (0);
986                 return code;
987         }
988
989         mono_trampolines_lock ();
990
991         if (!code)
992                 code = mono_arch_create_handler_block_trampoline ();
993
994         mono_trampolines_unlock ();
995
996         return code;
997 }
998 #endif
999
1000 /*
1001  * mono_get_trampoline_func:
1002  *
1003  *   Return the C function which needs to be called by the generic trampoline of type
1004  * TRAMP_TYPE.
1005  */
1006 gconstpointer
1007 mono_get_trampoline_func (MonoTrampolineType tramp_type)
1008 {
1009         switch (tramp_type) {
1010         case MONO_TRAMPOLINE_JIT:
1011         case MONO_TRAMPOLINE_JUMP:
1012                 return mono_magic_trampoline;
1013         case MONO_TRAMPOLINE_CLASS_INIT:
1014                 return mono_class_init_trampoline;
1015         case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
1016                 return mono_generic_class_init_trampoline;
1017         case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
1018                 return mono_rgctx_lazy_fetch_trampoline;
1019 #ifdef MONO_ARCH_AOT_SUPPORTED
1020         case MONO_TRAMPOLINE_AOT:
1021                 return mono_aot_trampoline;
1022         case MONO_TRAMPOLINE_AOT_PLT:
1023                 return mono_aot_plt_trampoline;
1024 #endif
1025 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1026         case MONO_TRAMPOLINE_DELEGATE:
1027                 return mono_delegate_trampoline;
1028 #endif
1029         case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
1030                 return mono_altstack_restore_prot;
1031         case MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING:
1032                 return mono_generic_virtual_remoting_trampoline;
1033         case MONO_TRAMPOLINE_MONITOR_ENTER:
1034                 return mono_monitor_enter_trampoline;
1035         case MONO_TRAMPOLINE_MONITOR_EXIT:
1036                 return mono_monitor_exit_trampoline;
1037         case MONO_TRAMPOLINE_VCALL:
1038                 return mono_vcall_trampoline;
1039 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1040         case MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD:
1041                 return mono_handler_block_guard_trampoline;
1042 #endif
1043         default:
1044                 g_assert_not_reached ();
1045                 return NULL;
1046         }
1047 }
1048
1049 static guchar*
1050 create_trampoline_code (MonoTrampolineType tramp_type)
1051 {
1052         MonoTrampInfo *info;
1053         guchar *code;
1054
1055         code = mono_arch_create_generic_trampoline (tramp_type, &info, FALSE);
1056         if (info) {
1057                 mono_save_trampoline_xdebug_info (info);
1058                 mono_tramp_info_free (info);
1059         }
1060
1061         return code;
1062 }
1063
1064 void
1065 mono_trampolines_init (void)
1066 {
1067         InitializeCriticalSection (&trampolines_mutex);
1068
1069         if (mono_aot_only)
1070                 return;
1071
1072         mono_trampoline_code [MONO_TRAMPOLINE_JIT] = create_trampoline_code (MONO_TRAMPOLINE_JIT);
1073         mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
1074         mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
1075         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
1076         mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
1077 #ifdef MONO_ARCH_AOT_SUPPORTED
1078         mono_trampoline_code [MONO_TRAMPOLINE_AOT] = create_trampoline_code (MONO_TRAMPOLINE_AOT);
1079         mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
1080 #endif
1081 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1082         mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
1083 #endif
1084         mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
1085         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING] = create_trampoline_code (MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING);
1086         mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_ENTER] = create_trampoline_code (MONO_TRAMPOLINE_MONITOR_ENTER);
1087         mono_trampoline_code [MONO_TRAMPOLINE_MONITOR_EXIT] = create_trampoline_code (MONO_TRAMPOLINE_MONITOR_EXIT);
1088         mono_trampoline_code [MONO_TRAMPOLINE_VCALL] = create_trampoline_code (MONO_TRAMPOLINE_VCALL);
1089 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1090         mono_trampoline_code [MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD] = create_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
1091         mono_create_handler_block_trampoline ();
1092 #endif
1093
1094         mono_counters_register ("Calls to trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &trampoline_calls);
1095 }
1096
1097 void
1098 mono_trampolines_cleanup (void)
1099 {
1100         if (class_init_hash_addr)
1101                 g_hash_table_destroy (class_init_hash_addr);
1102
1103         DeleteCriticalSection (&trampolines_mutex);
1104 }
1105
1106 guint8 *
1107 mono_get_trampoline_code (MonoTrampolineType tramp_type)
1108 {
1109         g_assert (mono_trampoline_code [tramp_type]);
1110
1111         return mono_trampoline_code [tramp_type];
1112 }
1113
1114 gpointer
1115 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
1116 {
1117         if (mono_aot_only)
1118                 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
1119         else
1120                 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
1121 }
1122
1123 gpointer
1124 mono_create_class_init_trampoline (MonoVTable *vtable)
1125 {
1126         gpointer code, ptr;
1127         MonoDomain *domain = vtable->domain;
1128
1129         g_assert (!vtable->klass->generic_container);
1130
1131         /* previously created trampoline code */
1132         mono_domain_lock (domain);
1133         ptr = 
1134                 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
1135                                                                   vtable);
1136         mono_domain_unlock (domain);
1137         if (ptr)
1138                 return ptr;
1139
1140         code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
1141
1142         ptr = mono_create_ftnptr (domain, code);
1143
1144         /* store trampoline address */
1145         mono_domain_lock (domain);
1146         g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
1147                                                           vtable, ptr);
1148         mono_domain_unlock (domain);
1149
1150         mono_trampolines_lock ();
1151         if (!class_init_hash_addr)
1152                 class_init_hash_addr = g_hash_table_new (NULL, NULL);
1153         g_hash_table_insert (class_init_hash_addr, ptr, vtable);
1154         mono_trampolines_unlock ();
1155
1156         return ptr;
1157 }
1158
1159 gpointer
1160 mono_create_generic_class_init_trampoline (void)
1161 {
1162 #ifdef MONO_ARCH_VTABLE_REG
1163         static gpointer code;
1164         MonoTrampInfo *info;
1165
1166         mono_trampolines_lock ();
1167
1168         if (!code) {
1169                 if (mono_aot_only)
1170                         /* get_named_code () might return an ftnptr, but our caller expects a direct pointer */
1171                         code = mono_get_addr_from_ftnptr (mono_aot_get_trampoline ("generic_class_init_trampoline"));
1172                 else {
1173                         code = mono_arch_create_generic_class_init_trampoline (&info, FALSE);
1174
1175                         if (info) {
1176                                 mono_save_trampoline_xdebug_info (info);
1177                                 mono_tramp_info_free (info);
1178                         }
1179                 }
1180         }
1181
1182         mono_trampolines_unlock ();
1183
1184         return code;
1185 #else
1186         g_assert_not_reached ();
1187 #endif
1188 }
1189
1190 gpointer
1191 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
1192 {
1193         MonoJitInfo *ji;
1194         gpointer code;
1195         guint32 code_size = 0;
1196
1197         code = mono_jit_find_compiled_method_with_jit_info (domain, method, &ji);
1198         /*
1199          * We cannot recover the correct type of a shared generic
1200          * method from its native code address, so we use the
1201          * trampoline instead.
1202          */
1203         if (code && !ji->has_generic_jit_info)
1204                 return code;
1205
1206         mono_domain_lock (domain);
1207         code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
1208         mono_domain_unlock (domain);
1209         if (code)
1210                 return code;
1211
1212         code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
1213         g_assert (code_size);
1214
1215         ji = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO);
1216         ji->code_start = code;
1217         ji->code_size = code_size;
1218         ji->method = method;
1219
1220         /*
1221          * mono_delegate_ctor needs to find the method metadata from the 
1222          * trampoline address, so we save it here.
1223          */
1224
1225         mono_jit_info_table_add (domain, ji);
1226
1227         mono_domain_lock (domain);
1228         g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
1229         mono_domain_unlock (domain);
1230
1231         return ji->code_start;
1232 }
1233
1234 gpointer
1235 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
1236 {
1237         gpointer tramp;
1238
1239         if (mono_aot_only) {
1240                 /* Avoid creating trampolines if possible */
1241                 gpointer code = mono_jit_find_compiled_method (domain, method);
1242                 
1243                 if (code)
1244                         return code;
1245         }
1246
1247         mono_domain_lock (domain);
1248         tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
1249         mono_domain_unlock (domain);
1250         if (tramp)
1251                 return tramp;
1252
1253         tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
1254         
1255         mono_domain_lock (domain);
1256         g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
1257         mono_domain_unlock (domain);
1258
1259         mono_jit_stats.method_trampolines++;
1260
1261         return tramp;
1262 }       
1263
1264 gpointer
1265 mono_create_jit_trampoline (MonoMethod *method)
1266 {
1267         return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
1268 }
1269
1270 gpointer
1271 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
1272 {
1273         gpointer tramp;
1274
1275         MonoDomain *domain = mono_domain_get ();
1276         guint8 *buf, *start;
1277
1278         buf = start = mono_domain_code_reserve (domain, 2 * sizeof (gpointer));
1279
1280         *(gpointer*)(gpointer)buf = image;
1281         buf += sizeof (gpointer);
1282         *(guint32*)(gpointer)buf = token;
1283
1284         tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
1285
1286         mono_jit_stats.method_trampolines++;
1287
1288         return tramp;
1289 }       
1290
1291 gpointer
1292 mono_create_delegate_trampoline (MonoClass *klass)
1293 {
1294 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
1295         MonoDomain *domain = mono_domain_get ();
1296         gpointer ptr;
1297         guint32 code_size = 0;
1298         gpointer *tramp_data;
1299         MonoMethod *invoke;
1300
1301         mono_domain_lock (domain);
1302         ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
1303         mono_domain_unlock (domain);
1304         if (ptr)
1305                 return ptr;
1306
1307         // Precompute the delegate invoke impl and pass it to the delegate trampoline
1308         invoke = mono_get_delegate_invoke (klass);
1309         g_assert (invoke);
1310
1311         tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
1312         tramp_data [0] = invoke;
1313         tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
1314         tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
1315
1316         ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
1317         g_assert (code_size);
1318
1319         /* store trampoline address */
1320         mono_domain_lock (domain);
1321         g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
1322                                                           klass, ptr);
1323         mono_domain_unlock (domain);
1324
1325         return ptr;
1326 #else
1327         return NULL;
1328 #endif
1329 }
1330
1331 gpointer
1332 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
1333 {
1334         static gboolean inited = FALSE;
1335         static int num_trampolines = 0;
1336         MonoTrampInfo *info;
1337
1338         gpointer tramp, ptr;
1339
1340         if (mono_aot_only)
1341                 return mono_aot_get_lazy_fetch_trampoline (offset);
1342
1343         mono_trampolines_lock ();
1344         if (rgctx_lazy_fetch_trampoline_hash)
1345                 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
1346         else
1347                 tramp = NULL;
1348         mono_trampolines_unlock ();
1349         if (tramp)
1350                 return tramp;
1351
1352         tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, FALSE);
1353         if (info) {
1354                 mono_save_trampoline_xdebug_info (info);
1355                 mono_tramp_info_free (info);
1356         }
1357         ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
1358
1359         mono_trampolines_lock ();
1360         if (!rgctx_lazy_fetch_trampoline_hash) {
1361                 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
1362                 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
1363         }
1364         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
1365         g_assert (offset != -1);
1366         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
1367         mono_trampolines_unlock ();
1368
1369         if (!inited) {
1370                 mono_counters_register ("RGCTX num lazy fetch trampolines",
1371                                 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
1372                 inited = TRUE;
1373         }
1374         num_trampolines++;
1375
1376         return ptr;
1377 }
1378
1379 gpointer
1380 mono_create_monitor_enter_trampoline (void)
1381 {
1382         static gpointer code;
1383
1384         if (mono_aot_only) {
1385                 if (!code)
1386                         code = mono_aot_get_trampoline ("monitor_enter_trampoline");
1387                 return code;
1388         }
1389
1390 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1391         mono_trampolines_lock ();
1392
1393         if (!code)
1394                 code = mono_arch_create_monitor_enter_trampoline (NULL, FALSE);
1395
1396         mono_trampolines_unlock ();
1397 #else
1398         code = NULL;
1399         g_assert_not_reached ();
1400 #endif
1401
1402         return code;
1403 }
1404
1405 gpointer
1406 mono_create_monitor_exit_trampoline (void)
1407 {
1408         static gpointer code;
1409
1410         if (mono_aot_only) {
1411                 if (!code)
1412                         code = mono_aot_get_trampoline ("monitor_exit_trampoline");
1413                 return code;
1414         }
1415
1416 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1417         mono_trampolines_lock ();
1418
1419         if (!code)
1420                 code = mono_arch_create_monitor_exit_trampoline (NULL, FALSE);
1421
1422         mono_trampolines_unlock ();
1423 #else
1424         code = NULL;
1425         g_assert_not_reached ();
1426 #endif
1427         return code;
1428 }
1429  
1430 #ifdef MONO_ARCH_LLVM_SUPPORTED
1431 /*
1432  * mono_create_llvm_imt_trampoline:
1433  *
1434  *   LLVM compiled code can't pass in the IMT argument, so we use this trampoline, which
1435  * sets the IMT argument, then branches to the contents of the vtable slot given by
1436  * vt_offset in the vtable which is obtained from the argument list.
1437  */
1438 gpointer
1439 mono_create_llvm_imt_trampoline (MonoDomain *domain, MonoMethod *m, int vt_offset)
1440 {
1441 #ifdef MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE
1442         return mono_arch_get_llvm_imt_trampoline (domain, m, vt_offset);
1443 #else
1444         g_assert_not_reached ();
1445         return NULL;
1446 #endif
1447 }
1448 #endif
1449
1450 MonoVTable*
1451 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
1452 {
1453         MonoVTable *res;
1454
1455         mono_trampolines_lock ();
1456         if (class_init_hash_addr)
1457                 res = g_hash_table_lookup (class_init_hash_addr, addr);
1458         else
1459                 res = NULL;
1460         mono_trampolines_unlock ();
1461         return res;
1462 }
1463
1464 guint32
1465 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
1466 {
1467         int offset;
1468
1469         mono_trampolines_lock ();
1470         if (rgctx_lazy_fetch_trampoline_hash_addr) {
1471                 /* We store the real offset + 1 so we can detect when the lookup fails */
1472                 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
1473                 if (offset)
1474                         offset -= 1;
1475                 else
1476                         offset = -1;
1477         } else {
1478                 offset = -1;
1479         }
1480         mono_trampolines_unlock ();
1481         return offset;
1482 }
1483
1484 static const char*tramp_names [MONO_TRAMPOLINE_NUM] = {
1485         "jit",
1486         "jump",
1487         "class_init",
1488         "generic_class_init",
1489         "rgctx_lazy_fetch",
1490         "aot",
1491         "aot_plt",
1492         "delegate",
1493         "restore_stack_prot",
1494         "generic_virtual_remoting",
1495         "monitor_enter",
1496         "monitor_exit",
1497         "vcall",
1498 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
1499         "handler_block_guard"
1500 #endif
1501 };
1502
1503 /*
1504  * mono_get_generic_trampoline_name:
1505  *
1506  *   Returns a pointer to malloc-ed memory.
1507  */
1508 char*
1509 mono_get_generic_trampoline_name (MonoTrampolineType tramp_type)
1510 {
1511         return g_strdup_printf ("generic_trampoline_%s", tramp_names [tramp_type]);
1512 }
1513
1514 /*
1515  * mono_get_rgctx_fetch_trampoline_name:
1516  *
1517  *   Returns a pointer to malloc-ed memory.
1518  */
1519 char*
1520 mono_get_rgctx_fetch_trampoline_name (int slot)
1521 {
1522         gboolean mrgctx;
1523         int index;
1524
1525         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
1526         index = MONO_RGCTX_SLOT_INDEX (slot);
1527
1528         return g_strdup_printf ("rgctx_fetch_trampoline_%s_%d", mrgctx ? "mrgctx" : "rgctx", index);
1529 }
1530