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