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