Added Changelog entry.
[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         guint8 *plt_entry;
386
387         image = *(gpointer*)(gpointer)token_info;
388         token_info += sizeof (gpointer);
389         token = *(guint32*)(gpointer)token_info;
390
391         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
392         if (!addr) {
393                 method = mono_get_method (image, token, NULL);
394                 g_assert (method);
395
396                 /* Use the generic code */
397                 return mono_magic_trampoline (regs, code, method, tramp);
398         }
399
400         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
401         g_assert (!vtable_slot);
402
403         /* This is a normal call through a PLT entry */
404         plt_entry = mono_aot_get_plt_entry (code);
405         g_assert (plt_entry);
406
407         mono_arch_patch_plt_entry (plt_entry, addr);
408
409         is_got_entry = FALSE;
410
411         /*
412          * Since AOT code is only used in the root domain, 
413          * mono_domain_get () != mono_get_root_domain () means the calling method
414          * is AppDomain:InvokeInDomain, so this is the same check as in 
415          * mono_method_same_domain () but without loading the metadata for the method.
416          */
417         if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
418 #ifdef MONO_ARCH_HAVE_IMT
419                 if (!method)
420                         method = mono_get_method (image, token, NULL);
421                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
422 #endif
423                 *vtable_slot = addr;
424         }
425
426         return addr;
427 }
428
429 /*
430  * mono_aot_plt_trampoline:
431  *
432  *   This trampoline handles calls made from AOT code through the PLT table.
433  */
434 gpointer
435 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module, 
436                                                  guint8* tramp)
437 {
438         guint32 plt_info_offset = mono_aot_get_plt_info_offset (regs, code);
439
440         return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
441 }
442 #endif
443
444 /**
445  * mono_class_init_trampoline:
446  *
447  * This method calls mono_runtime_class_init () to run the static constructor
448  * for the type, then patches the caller code so it is not called again.
449  */
450 void
451 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
452 {
453         guint8 *plt_entry = mono_aot_get_plt_entry (code);
454
455         mono_runtime_class_init (vtable);
456
457         if (!mono_running_on_valgrind ()) {
458                 if (plt_entry) {
459                         mono_arch_nullify_plt_entry (plt_entry);
460                 } else {
461                         mono_arch_nullify_class_init_trampoline (code, regs);
462                 }
463         }
464 }
465
466 /**
467  * mono_generic_class_init_trampoline:
468  *
469  * This method calls mono_runtime_class_init () to run the static constructor
470  * for the type.
471  */
472 void
473 mono_generic_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
474 {
475         g_assert (!vtable->initialized);
476
477         mono_runtime_class_init (vtable);
478 }
479
480 static gpointer
481 mono_rgctx_lazy_fetch_trampoline (gssize *regs, guint8 *code, gpointer data, guint8 *tramp)
482 {
483 #ifdef MONO_ARCH_VTABLE_REG
484         static gboolean inited = FALSE;
485         static int num_lookups = 0;
486         guint32 slot = GPOINTER_TO_UINT (data);
487         gpointer arg = (gpointer)(gssize)regs [MONO_ARCH_VTABLE_REG];
488         guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
489         gboolean mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
490
491         if (!inited) {
492                 mono_counters_register ("RGCTX unmanaged lookups", MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_lookups);
493                 inited = TRUE;
494         }
495
496         num_lookups++;
497
498         if (mrgctx)
499                 return mono_method_fill_runtime_generic_context (arg, index);
500         else
501                 return mono_class_fill_runtime_generic_context (arg, index);
502 #else
503         g_assert_not_reached ();
504 #endif
505 }
506
507 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
508
509 /**
510  * mono_delegate_trampoline:
511  *
512  *   This trampoline handles calls made to Delegate:Invoke ().
513  * This is called once the first time a delegate is invoked, so it must be fast.
514  */
515 gpointer
516 mono_delegate_trampoline (gssize *regs, guint8 *code, gpointer *tramp_data, guint8* tramp)
517 {
518         MonoDomain *domain = mono_domain_get ();
519         MonoDelegate *delegate;
520         MonoJitInfo *ji;
521         MonoMethod *m;
522         MonoMethod *method = NULL;
523         gboolean multicast, callvirt;
524         MonoMethod *invoke = tramp_data [0];
525         guint8 *impl_this = tramp_data [1];
526         guint8 *impl_nothis = tramp_data [2];
527
528         /* Obtain the delegate object according to the calling convention */
529
530         /* 
531          * Avoid calling mono_get_generic_context_from_code () now since it is expensive, 
532          * get_this_arg_from_call will call it if needed.
533          */
534         delegate = mono_arch_get_this_arg_from_call (NULL, mono_method_signature (invoke), regs, code);
535
536         if (delegate->method) {
537                 method = delegate->method;
538
539                 /*
540                  * delegate->method_ptr == NULL means the delegate was initialized by 
541                  * mini_delegate_ctor, while != NULL means it is initialized by 
542                  * mono_delegate_ctor_with_method (). In both cases, we need to add wrappers
543                  * (ctor_with_method () does this, but it doesn't store the wrapper back into
544                  * delegate->method).
545                  */
546                 if (delegate->target && delegate->target->vtable->klass == mono_defaults.transparent_proxy_class)
547                         method = mono_marshal_get_remoting_invoke (method);
548                 else if (mono_method_signature (method)->hasthis && method->klass->valuetype)
549                         method = mono_marshal_get_unbox_wrapper (method);
550         } else {
551                 ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
552                 if (ji)
553                         method = ji->method;
554         }
555         callvirt = !delegate->target && method && mono_method_signature (method)->hasthis;
556
557         if (method && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
558                 method = mono_marshal_get_synchronized_wrapper (method);
559
560         /* 
561          * If the called address is a trampoline, replace it with the compiled method so
562          * further calls don't have to go through the trampoline.
563          */
564         if (method && !callvirt) {
565                 /* Avoid the overhead of looking up an already compiled method if possible */
566                 if (delegate->method_code && *delegate->method_code) {
567                         delegate->method_ptr = *delegate->method_code;
568                 } else {
569                         delegate->method_ptr = mono_compile_method (method);
570                         if (delegate->method_code)
571                                 *delegate->method_code = delegate->method_ptr;
572                         mono_debugger_trampoline_compiled (method, delegate->method_ptr);
573                 }
574         }
575
576         multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
577         if (!multicast && !callvirt) {
578                 code = delegate->target ? impl_this : impl_nothis;
579
580                 if (code) {
581                         delegate->invoke_impl = code;
582                         return code;
583                 }
584         }
585
586         /* The general, unoptimized case */
587         m = mono_marshal_get_delegate_invoke (invoke, delegate);
588         code = mono_compile_method (m);
589         delegate->invoke_impl = mono_get_addr_from_ftnptr (code);
590         mono_debugger_trampoline_compiled (m, delegate->invoke_impl);
591
592         return code;
593 }
594
595 #endif
596
597 /*
598  * mono_get_trampoline_func:
599  *
600  *   Return the C function which needs to be called by the generic trampoline of type
601  * TRAMP_TYPE.
602  */
603 gconstpointer
604 mono_get_trampoline_func (MonoTrampolineType tramp_type)
605 {
606         switch (tramp_type) {
607         case MONO_TRAMPOLINE_JIT:
608         case MONO_TRAMPOLINE_JUMP:
609                 return mono_magic_trampoline;
610         case MONO_TRAMPOLINE_CLASS_INIT:
611                 return mono_class_init_trampoline;
612         case MONO_TRAMPOLINE_GENERIC_CLASS_INIT:
613                 return mono_generic_class_init_trampoline;
614         case MONO_TRAMPOLINE_RGCTX_LAZY_FETCH:
615                 return mono_rgctx_lazy_fetch_trampoline;
616 #ifdef MONO_ARCH_AOT_SUPPORTED
617         case MONO_TRAMPOLINE_AOT:
618                 return mono_aot_trampoline;
619         case MONO_TRAMPOLINE_AOT_PLT:
620                 return mono_aot_plt_trampoline;
621 #endif
622 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
623         case MONO_TRAMPOLINE_DELEGATE:
624                 return mono_delegate_trampoline;
625 #endif
626         case MONO_TRAMPOLINE_RESTORE_STACK_PROT:
627                 return mono_altstack_restore_prot;
628         default:
629                 g_assert_not_reached ();
630                 return NULL;
631         }
632 }
633
634 void
635 mono_trampolines_init (void)
636 {
637         InitializeCriticalSection (&trampolines_mutex);
638
639         if (mono_aot_only)
640                 return;
641
642         mono_trampoline_code [MONO_TRAMPOLINE_JIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JIT);
643         mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
644         mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
645         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
646         mono_trampoline_code [MONO_TRAMPOLINE_RGCTX_LAZY_FETCH] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RGCTX_LAZY_FETCH);
647 #ifdef MONO_ARCH_AOT_SUPPORTED
648         mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
649         mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
650 #endif
651 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
652         mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
653 #endif
654         mono_trampoline_code [MONO_TRAMPOLINE_RESTORE_STACK_PROT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_RESTORE_STACK_PROT);
655 }
656
657 void
658 mono_trampolines_cleanup (void)
659 {
660         if (class_init_hash_addr)
661                 g_hash_table_destroy (class_init_hash_addr);
662         if (delegate_trampoline_hash_addr)
663                 g_hash_table_destroy (delegate_trampoline_hash_addr);
664
665         DeleteCriticalSection (&trampolines_mutex);
666 }
667
668 guint8 *
669 mono_get_trampoline_code (MonoTrampolineType tramp_type)
670 {
671         g_assert (mono_trampoline_code [tramp_type]);
672
673         return mono_trampoline_code [tramp_type];
674 }
675
676 gpointer
677 mono_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
678 {
679         if (mono_aot_only)
680                 return mono_aot_create_specific_trampoline (mono_defaults.corlib, arg1, tramp_type, domain, code_len);
681         else
682                 return mono_arch_create_specific_trampoline (arg1, tramp_type, domain, code_len);
683 }
684
685 gpointer
686 mono_create_class_init_trampoline (MonoVTable *vtable)
687 {
688         gpointer code, ptr;
689         MonoDomain *domain = vtable->domain;
690
691         g_assert (!vtable->klass->generic_container);
692
693         /* previously created trampoline code */
694         mono_domain_lock (domain);
695         ptr = 
696                 g_hash_table_lookup (domain_jit_info (domain)->class_init_trampoline_hash,
697                                                                   vtable);
698         mono_domain_unlock (domain);
699         if (ptr)
700                 return ptr;
701
702         code = mono_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, domain, NULL);
703
704         ptr = mono_create_ftnptr (domain, code);
705
706         /* store trampoline address */
707         mono_domain_lock (domain);
708         g_hash_table_insert (domain_jit_info (domain)->class_init_trampoline_hash,
709                                                           vtable, ptr);
710         mono_domain_unlock (domain);
711
712         mono_trampolines_lock ();
713         if (!class_init_hash_addr)
714                 class_init_hash_addr = g_hash_table_new (NULL, NULL);
715         g_hash_table_insert (class_init_hash_addr, ptr, vtable);
716         mono_trampolines_unlock ();
717
718         return ptr;
719 }
720
721 gpointer
722 mono_create_generic_class_init_trampoline (void)
723 {
724 #ifdef MONO_ARCH_VTABLE_REG
725         static gpointer code;
726
727         mono_trampolines_lock ();
728
729         if (!code)
730                 code = mono_arch_create_generic_class_init_trampoline ();
731
732         mono_trampolines_unlock ();
733
734         return code;
735 #else
736         g_assert_not_reached ();
737 #endif
738 }
739
740 gpointer
741 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, gboolean add_sync_wrapper)
742 {
743         MonoJitInfo *ji;
744         gpointer code;
745         guint32 code_size = 0;
746
747         code = mono_jit_find_compiled_method (domain, method);
748         if (code)
749                 return code;
750
751         mono_domain_lock (domain);
752         code = g_hash_table_lookup (domain_jit_info (domain)->jump_trampoline_hash, method);
753         mono_domain_unlock (domain);
754         if (code)
755                 return code;
756
757         code = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
758         g_assert (code_size);
759
760         mono_domain_lock (domain);
761         ji = mono_domain_alloc0 (domain, sizeof (MonoJitInfo));
762         mono_domain_unlock (domain);
763         ji->code_start = code;
764         ji->code_size = code_size;
765         ji->method = method;
766
767         /*
768          * mono_delegate_ctor needs to find the method metadata from the 
769          * trampoline address, so we save it here.
770          */
771
772         mono_jit_info_table_add (domain, ji);
773
774         mono_domain_lock (domain);
775         g_hash_table_insert (domain_jit_info (domain)->jump_trampoline_hash, method, ji->code_start);
776         mono_domain_unlock (domain);
777
778         return ji->code_start;
779 }
780
781 gpointer
782 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
783 {
784         gpointer tramp;
785
786         if (mono_aot_only) {
787                 /* Avoid creating trampolines if possible */
788                 gpointer code = mono_jit_find_compiled_method (domain, method);
789                 
790                 if (code)
791                         return code;
792         }
793
794         mono_domain_lock (domain);
795         tramp = g_hash_table_lookup (domain_jit_info (domain)->jit_trampoline_hash, method);
796         mono_domain_unlock (domain);
797         if (tramp)
798                 return tramp;
799
800         tramp = mono_create_specific_trampoline (method, MONO_TRAMPOLINE_JIT, domain, NULL);
801         
802         mono_domain_lock (domain);
803         g_hash_table_insert (domain_jit_info (domain)->jit_trampoline_hash, method, tramp);
804         mono_domain_unlock (domain);
805
806         mono_jit_stats.method_trampolines++;
807
808         return tramp;
809 }       
810
811 gpointer
812 mono_create_jit_trampoline (MonoMethod *method)
813 {
814         return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
815 }
816
817 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
818 gpointer
819 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
820 {
821         gpointer tramp;
822
823         MonoDomain *domain = mono_domain_get ();
824         guint8 *buf, *start;
825
826         mono_domain_lock (domain);
827         buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
828         mono_domain_unlock (domain);
829
830         *(gpointer*)(gpointer)buf = image;
831         buf += sizeof (gpointer);
832         *(guint32*)(gpointer)buf = token;
833
834         tramp = mono_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
835
836         mono_jit_stats.method_trampolines++;
837
838         return tramp;
839 }       
840 #endif
841
842 gpointer
843 mono_create_delegate_trampoline (MonoClass *klass)
844 {
845 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
846         MonoDomain *domain = mono_domain_get ();
847         gpointer ptr;
848         guint32 code_size = 0;
849         gpointer *tramp_data;
850         MonoMethod *invoke;
851
852         mono_domain_lock (domain);
853         ptr = g_hash_table_lookup (domain_jit_info (domain)->delegate_trampoline_hash, klass);
854         mono_domain_unlock (domain);
855         if (ptr)
856                 return ptr;
857
858         // Precompute the delegate invoke impl and pass it to the delegate trampoline
859         invoke = mono_get_delegate_invoke (klass);
860         g_assert (invoke);
861
862         mono_domain_lock (domain );
863         tramp_data = mono_domain_alloc (domain, sizeof (gpointer) * 3);
864         mono_domain_unlock (domain);
865         tramp_data [0] = invoke;
866         if (mono_aot_only) {
867                 tramp_data [1] = NULL;
868                 tramp_data [2] = NULL;
869         } else {
870                 tramp_data [1] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), TRUE);
871                 tramp_data [2] = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), FALSE);
872         }
873
874         ptr = mono_create_specific_trampoline (tramp_data, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
875         g_assert (code_size);
876
877         /* store trampoline address */
878         mono_domain_lock (domain);
879         g_hash_table_insert (domain_jit_info (domain)->delegate_trampoline_hash,
880                                                           klass, ptr);
881         mono_domain_unlock (domain);
882
883         mono_trampolines_lock ();
884         if (!delegate_trampoline_hash_addr)
885                 delegate_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
886         g_hash_table_insert (delegate_trampoline_hash_addr, ptr, klass);
887         mono_trampolines_unlock ();
888
889         return ptr;
890 #else
891         return NULL;
892 #endif
893 }
894
895 gpointer
896 mono_create_rgctx_lazy_fetch_trampoline (guint32 offset)
897 {
898         static gboolean inited = FALSE;
899         static int num_trampolines = 0;
900
901         gpointer tramp, ptr;
902
903         if (mono_aot_only)
904                 return mono_aot_get_lazy_fetch_trampoline (offset);
905
906         mono_trampolines_lock ();
907         if (rgctx_lazy_fetch_trampoline_hash)
908                 tramp = g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset));
909         else
910                 tramp = NULL;
911         mono_trampolines_unlock ();
912         if (tramp)
913                 return tramp;
914
915         tramp = mono_arch_create_rgctx_lazy_fetch_trampoline (offset);
916         ptr = mono_create_ftnptr (mono_get_root_domain (), tramp);
917
918         mono_trampolines_lock ();
919         if (!rgctx_lazy_fetch_trampoline_hash) {
920                 rgctx_lazy_fetch_trampoline_hash = g_hash_table_new (NULL, NULL);
921                 rgctx_lazy_fetch_trampoline_hash_addr = g_hash_table_new (NULL, NULL);
922         }
923         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash, GUINT_TO_POINTER (offset), ptr);
924         g_assert (offset != -1);
925         g_hash_table_insert (rgctx_lazy_fetch_trampoline_hash_addr, ptr, GUINT_TO_POINTER (offset + 1));
926         mono_trampolines_unlock ();
927
928         if (!inited) {
929                 mono_counters_register ("RGCTX num lazy fetch trampolines",
930                                 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &num_trampolines);
931                 inited = TRUE;
932         }
933         num_trampolines++;
934
935         return ptr;
936 }
937
938 MonoVTable*
939 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
940 {
941         MonoVTable *res;
942
943         mono_trampolines_lock ();
944         if (class_init_hash_addr)
945                 res = g_hash_table_lookup (class_init_hash_addr, addr);
946         else
947                 res = NULL;
948         mono_trampolines_unlock ();
949         return res;
950 }
951
952 MonoClass*
953 mono_find_delegate_trampoline_by_addr (gconstpointer addr)
954 {
955         MonoClass *res;
956
957         mono_trampolines_lock ();
958         if (delegate_trampoline_hash_addr)
959                 res = g_hash_table_lookup (delegate_trampoline_hash_addr, addr);
960         else
961                 res = NULL;
962         mono_trampolines_unlock ();
963         return res;
964 }
965
966 guint32
967 mono_find_rgctx_lazy_fetch_trampoline_by_addr (gconstpointer addr)
968 {
969         int offset;
970
971         mono_trampolines_lock ();
972         if (rgctx_lazy_fetch_trampoline_hash_addr) {
973                 /* We store the real offset + 1 so we can detect when the lookup fails */
974                 offset = GPOINTER_TO_INT (g_hash_table_lookup (rgctx_lazy_fetch_trampoline_hash_addr, addr));
975                 if (offset)
976                         offset -= 1;
977                 else
978                         offset = -1;
979         } else {
980                 offset = -1;
981         }
982         mono_trampolines_unlock ();
983         return offset;
984 }