2007-10-30 Martin Baulig <martin@ximian.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
10 #ifdef HAVE_VALGRIND_MEMCHECK_H
11 #include <valgrind/memcheck.h>
12 #endif
13
14 #include "mini.h"
15
16 #ifdef MONO_ARCH_HAVE_IMT
17
18 static gpointer*
19 mono_convert_imt_slot_to_vtable_slot (gpointer* slot, gpointer *regs, guint8 *code, MonoMethod *method, MonoMethod **impl_method)
20 {
21         MonoObject *this_argument = mono_arch_find_this_argument (regs, method);
22         MonoVTable *vt = this_argument->vtable;
23         int displacement = slot - ((gpointer*)vt);
24         
25         if (displacement > 0) {
26                 /* slot is in the vtable, not in the IMT */
27 #if DEBUG_IMT
28                 printf ("mono_convert_imt_slot_to_vtable_slot: slot %p is in the vtable, not in the IMT\n", slot);
29 #endif
30                 return slot;
31         } else {
32                 MonoMethod *imt_method = mono_arch_find_imt_method (regs, code);
33                 int interface_offset;
34                 int imt_slot = MONO_IMT_SIZE + displacement;
35
36                 mono_class_setup_vtable (vt->klass);
37                 interface_offset = mono_class_interface_offset (vt->klass, imt_method->klass);
38
39                 if (interface_offset < 0) {
40                         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));
41                         g_assert_not_reached ();
42                 }
43                 mono_vtable_build_imt_slot (vt, mono_method_get_imt_slot (imt_method));
44
45                 if (impl_method)
46                         *impl_method = vt->klass->vtable [interface_offset + imt_method->slot];
47 #if DEBUG_IMT
48                 printf ("mono_convert_imt_slot_to_vtable_slot: method = %s.%s.%s, imt_method = %s.%s.%s\n",
49                                 method->klass->name_space, method->klass->name, method->name, 
50                                 imt_method->klass->name_space, imt_method->klass->name, imt_method->name);
51 #endif
52                 g_assert (imt_slot < MONO_IMT_SIZE);
53                 if (vt->imt_collisions_bitmap & (1 << imt_slot)) {
54                         int vtable_offset = interface_offset + imt_method->slot;
55                         gpointer *vtable_slot = & (vt->vtable [vtable_offset]);
56 #if DEBUG_IMT
57                         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);
58 #endif
59                         return vtable_slot;
60                 } else {
61 #if DEBUG_IMT
62                         printf ("mono_convert_imt_slot_to_vtable_slot: slot %p[%d] is in the IMT, but not colliding\n", slot, imt_slot);
63 #endif
64                         return slot;
65                 }
66         }
67 }
68 #endif
69
70 /**
71  * mono_magic_trampoline:
72  *
73  *   This trampoline handles calls from JITted code.
74  */
75 gpointer
76 mono_magic_trampoline (gssize *regs, guint8 *code, MonoMethod *m, guint8* tramp)
77 {
78         gpointer addr;
79         gpointer *vtable_slot;
80
81 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
82         if (m == MONO_FAKE_VTABLE_METHOD) {
83                 int displacement;
84                 MonoVTable *vt = mono_arch_get_vcall_slot (code, (gpointer*)regs, &displacement);
85                 g_assert (vt);
86                 if (displacement > 0) {
87                         displacement -= G_STRUCT_OFFSET (MonoVTable, vtable);
88                         g_assert (displacement >= 0);
89                         displacement /= sizeof (gpointer);
90                         mono_class_setup_vtable (vt->klass);
91                         m = vt->klass->vtable [displacement];
92                         if (m->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
93                                 m = mono_marshal_get_synchronized_wrapper (m);
94                         /*g_print ("%s with disp %d: %s at %p\n", vt->klass->name, displacement, m->name, code);*/
95                 } else {
96                         /* We got here from an interface method: redirect to IMT handling */
97                         m = MONO_FAKE_IMT_METHOD;
98                         /*g_print ("vtable with disp %d at %p\n", displacement, code);*/
99                 }
100         }
101 #endif
102         /* this is the IMT trampoline */
103 #ifdef MONO_ARCH_HAVE_IMT
104         if (m == MONO_FAKE_IMT_METHOD) {
105                 MonoMethod *impl_method;
106                 /* we get the interface method because mono_convert_imt_slot_to_vtable_slot ()
107                  * needs the signature to be able to find the this argument
108                  */
109                 m = mono_arch_find_imt_method (regs, code);
110                 vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
111                 g_assert (vtable_slot);
112                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, &impl_method);
113                 /* mono_convert_imt_slot_to_vtable_slot () also gives us the method that is supposed
114                  * to be called, so we compile it and go ahead as usual.
115                  */
116                 /*g_print ("imt found method %p (%s) at %p\n", impl_method, impl_method->name, code);*/
117                 m = impl_method;
118         }
119 #endif
120
121         addr = mono_compile_method (m);
122         g_assert (addr);
123
124         mono_debugger_trampoline_compiled (m, addr);
125
126         /* the method was jumped to */
127         if (!code)
128                 return addr;
129
130         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
131
132         if (vtable_slot) {
133                 if (m->klass->valuetype)
134                         addr = mono_arch_get_unbox_trampoline (m, addr);
135
136                 g_assert (*vtable_slot);
137
138                 if (mono_aot_is_got_entry (code, (guint8*)vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
139 #ifdef MONO_ARCH_HAVE_IMT
140                         vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, m, NULL);
141 #endif
142                         *vtable_slot = mono_get_addr_from_ftnptr (addr);
143                 }
144         }
145         else {
146                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
147
148                 /* Patch calling code */
149                 if (plt_entry) {
150                         mono_arch_patch_plt_entry (plt_entry, addr);
151                 } else {
152                         MonoJitInfo *ji = 
153                                 mono_jit_info_table_find (mono_domain_get (), (char*)code);
154                         MonoJitInfo *target_ji = 
155                                 mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (addr));
156
157                         if (mono_method_same_domain (ji, target_ji))
158                                 mono_arch_patch_callsite (code, addr);
159                 }
160         }
161
162         return addr;
163 }
164
165 /*
166  * mono_aot_trampoline:
167  *
168  *   This trampoline handles calls made from AOT code. We try to bypass the 
169  * normal JIT compilation logic to avoid loading the metadata for the method.
170  */
171 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
172 gpointer
173 mono_aot_trampoline (gssize *regs, guint8 *code, guint8 *token_info, 
174                                          guint8* tramp)
175 {
176         MonoImage *image;
177         guint32 token;
178         MonoMethod *method = NULL;
179         gpointer addr;
180         gpointer *vtable_slot;
181         gboolean is_got_entry;
182
183         image = *(gpointer*)(gpointer)token_info;
184         token_info += sizeof (gpointer);
185         token = *(guint32*)(gpointer)token_info;
186
187         addr = mono_aot_get_method_from_token (mono_domain_get (), image, token);
188         if (!addr) {
189                 method = mono_get_method (image, token, NULL);
190                 g_assert (method);
191
192                 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
193
194                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
195                         method = mono_marshal_get_synchronized_wrapper (method);
196
197                 addr = mono_compile_method (method);
198                 g_assert (addr);
199         }
200
201         vtable_slot = mono_arch_get_vcall_slot_addr (code, (gpointer*)regs);
202
203         if (vtable_slot) {
204                 is_got_entry = mono_aot_is_got_entry (code, (guint8*)vtable_slot);
205
206                 if (!is_got_entry) {
207                         if (!method)
208                                 method = mono_get_method (image, token, NULL);
209                         if (method->klass->valuetype)
210                                 addr = mono_arch_get_unbox_trampoline (method, addr);
211                 }
212         } else {
213                 /* This is a normal call through a PLT entry */
214                 guint8 *plt_entry = mono_aot_get_plt_entry (code);
215
216                 g_assert (plt_entry);
217
218                 mono_arch_patch_plt_entry (plt_entry, addr);
219
220                 is_got_entry = FALSE;
221         }
222
223         /*
224          * Since AOT code is only used in the root domain, 
225          * mono_domain_get () != mono_get_root_domain () means the calling method
226          * is AppDomain:InvokeInDomain, so this is the same check as in 
227          * mono_method_same_domain () but without loading the metadata for the method.
228          */
229         if ((is_got_entry && (mono_domain_get () == mono_get_root_domain ())) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot)) {
230 #ifdef MONO_ARCH_HAVE_IMT
231                 if (!method)
232                         method = mono_get_method (image, token, NULL);
233                 vtable_slot = mono_convert_imt_slot_to_vtable_slot (vtable_slot, (gpointer*)regs, code, method, NULL);
234 #endif
235                 *vtable_slot = addr;
236         }
237
238         return addr;
239 }
240
241 /*
242  * mono_aot_plt_trampoline:
243  *
244  *   This trampoline handles calls made from AOT code through the PLT table.
245  */
246 gpointer
247 mono_aot_plt_trampoline (gssize *regs, guint8 *code, guint8 *aot_module, 
248                                                  guint8* tramp)
249 {
250 #ifdef MONO_ARCH_AOT_PLT_OFFSET_REG
251         guint32 plt_info_offset = regs [MONO_ARCH_AOT_PLT_OFFSET_REG];
252 #else
253         guint32 plt_info_offset = -1;
254 #endif
255
256         return mono_aot_plt_resolve (aot_module, plt_info_offset, code);
257 }
258 #endif
259
260 /**
261  * mono_class_init_trampoline:
262  *
263  * This method calls mono_runtime_class_init () to run the static constructor
264  * for the type, then patches the caller code so it is not called again.
265  */
266 void
267 mono_class_init_trampoline (gssize *regs, guint8 *code, MonoVTable *vtable, guint8 *tramp)
268 {
269         guint8 *plt_entry = mono_aot_get_plt_entry (code);
270
271         mono_runtime_class_init (vtable);
272
273         if (!mono_running_on_valgrind ()) {
274                 if (plt_entry) {
275                         mono_arch_nullify_plt_entry (plt_entry);
276                 } else {
277                         mono_arch_nullify_class_init_trampoline (code, regs);
278                 }
279         }
280 }
281
282 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
283
284 /**
285  * mono_delegate_trampoline:
286  *
287  *   This trampoline handles calls made to Delegate:Invoke ().
288  */
289 gpointer
290 mono_delegate_trampoline (gssize *regs, guint8 *code, MonoClass *klass, guint8* tramp)
291 {
292         MonoDomain *domain = mono_domain_get ();
293         MonoDelegate *delegate;
294         MonoJitInfo *ji;
295         gpointer iter;
296         MonoMethod *invoke;
297         gboolean multicast;
298
299         /* Find the Invoke method */
300         iter = NULL;
301         while ((invoke = mono_class_get_methods (klass, &iter))) {
302                 if (!strcmp (invoke->name, "Invoke"))
303                         break;
304         }
305         g_assert (invoke);
306
307         /* Obtain the delegate object according to the calling convention */
308
309         delegate = mono_arch_get_this_arg_from_call (mono_method_signature (invoke), regs, code);
310
311         /* 
312          * If the called address is a trampoline, replace it with the compiled method so
313          * further calls don't have to go through the trampoline.
314          */
315         ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (delegate->method_ptr));
316         if (ji)
317                 delegate->method_ptr = mono_compile_method (ji->method);
318
319         multicast = ((MonoMulticastDelegate*)delegate)->prev != NULL;
320         if (!multicast) {
321                 code = mono_arch_get_delegate_invoke_impl (mono_method_signature (invoke), delegate->target != NULL);
322
323                 if (code) {
324                         delegate->invoke_impl = code;
325                         return code;
326                 }
327         }
328
329         /* The general, unoptimized case */
330         delegate->invoke_impl = mono_compile_method (mono_marshal_get_delegate_invoke (invoke));
331         return delegate->invoke_impl;
332 }
333
334 #endif
335
336