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