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