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