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