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