Wed Oct 31 19:29:30 CET 2007 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / tramp-arm.c
1 /*
2  * tramp-arm.c: JIT trampoline code for ARM
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/arch/arm/arm-codegen.h>
17
18 #include "mini.h"
19 #include "mini-arm.h"
20
21 /*
22  * Return the instruction to jump from code to target, 0 if not
23  * reachable with a single instruction
24  */
25 static guint32
26 branch_for_target_reachable (guint8 *branch, guint8 *target)
27 {
28         gint diff = target - branch - 8;
29         g_assert ((diff & 3) == 0);
30         if (diff >= 0) {
31                 if (diff <= 33554431)
32                         return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | (diff >> 2);
33         } else {
34                 /* diff between 0 and -33554432 */
35                 if (diff >= -33554432)
36                         return (ARMCOND_AL << ARMCOND_SHIFT) | (ARM_BR_TAG) | ((diff >> 2) & ~0xff000000);
37         }
38         return 0;
39 }
40
41 /*
42  * mono_arch_get_unbox_trampoline:
43  * @m: method pointer
44  * @addr: pointer to native code for @m
45  *
46  * when value type methods are called through the vtable we need to unbox the
47  * this argument. This method returns a pointer to a trampoline which does
48  * unboxing before calling the method
49  */
50 gpointer
51 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
52 {
53         guint8 *code, *start;
54         int this_pos = 0;
55         MonoDomain *domain = mono_domain_get ();
56
57         if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
58                 this_pos = 1;
59
60         mono_domain_lock (domain);
61         start = code = mono_code_manager_reserve (domain->code_mp, 16);
62         mono_domain_unlock (domain);
63
64         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
65         ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
66         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
67         *(guint32*)code = (guint32)addr;
68         code += 4;
69         mono_arch_flush_icache (start, code - start);
70         g_assert ((code - start) <= 16);
71         /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
72         g_print ("unbox code is at %p for method at %p\n", start, addr);*/
73
74         return start;
75 }
76
77 void
78 mono_arch_patch_callsite (guint8 *code_ptr, guint8 *addr)
79 {
80         guint32 *code = (guint32*)code_ptr;
81
82         /* This is the 'bl' or the 'mov pc' instruction */
83         --code;
84         
85         /*
86          * Note that methods are called also with the bl opcode.
87          */
88         if ((((*code) >> 25)  & 7) == 5) {
89                 /*g_print ("direct patching\n");*/
90                 arm_patch ((char*)code, addr);
91                 mono_arch_flush_icache ((char*)code, 4);
92                 return;
93         }
94
95         g_assert_not_reached ();
96 }
97
98 void
99 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
100 {
101         g_assert_not_reached ();
102 }
103
104 void
105 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
106 {
107         return;
108 }
109
110 void
111 mono_arch_nullify_plt_entry (guint8 *code)
112 {
113         g_assert_not_reached ();
114 }
115
116
117 /* Stack size for trampoline function 
118  */
119 #define STACK (sizeof (MonoLMF))
120
121 /* Method-specific trampoline code fragment size */
122 #define METHOD_TRAMPOLINE_SIZE 64
123
124 /* Jump-specific trampoline code fragment size */
125 #define JUMP_TRAMPOLINE_SIZE   64
126
127 #define GEN_TRAMP_SIZE 148
128
129 /*
130  * Stack frame description when the generic trampoline is called.
131  * caller frame
132  * ------------------- old sp
133  *  MonoLMF
134  * ------------------- sp
135  */
136 guchar*
137 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
138 {
139         guint8 *buf, *code = NULL;
140         int i, offset;
141         guint8 *load_get_lmf_addr, *load_trampoline;
142         gpointer *constants;
143
144         /* Now we'll create in 'buf' the ARM trampoline code. This
145          is the trampoline code common to all methods  */
146         
147         code = buf = mono_global_codeman_reserve (GEN_TRAMP_SIZE);
148
149         /*
150          * At this point lr points to the specific arg and sp points to the saved
151          * regs on the stack (all but PC and SP). The original LR value has been
152          * saved as sp + LR_OFFSET by the push in the specific trampoline
153          */
154 #define LR_OFFSET (sizeof (gpointer) * 13)
155         ARM_MOV_REG_REG (buf, ARMREG_V1, ARMREG_SP);
156         ARM_LDR_IMM (buf, ARMREG_V2, ARMREG_LR, 0);
157         ARM_LDR_IMM (buf, ARMREG_V3, ARMREG_SP, LR_OFFSET);
158
159         /* ok, now we can continue with the MonoLMF setup, mostly untouched 
160          * from emit_prolog in mini-arm.c
161          * This is a sinthetized call to mono_get_lmf_addr ()
162          */
163         load_get_lmf_addr = buf;
164         buf += 4;
165         ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
166         ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_R0);
167
168         /* we build the MonoLMF structure on the stack - see mini-arm.h
169          * The pointer to the struct is put in r1.
170          * the iregs array is already allocated on the stack by push.
171          */
172         ARM_SUB_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
173         ARM_ADD_REG_IMM8 (buf, ARMREG_R1, ARMREG_SP, STACK - sizeof (MonoLMF));
174         /* r0 is the result from mono_get_lmf_addr () */
175         ARM_STR_IMM (buf, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
176         /* new_lmf->previous_lmf = *lmf_addr */
177         ARM_LDR_IMM (buf, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
178         ARM_STR_IMM (buf, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
179         /* *(lmf_addr) = r1 */
180         ARM_STR_IMM (buf, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
181         /* save method info (it's in v2) */
182         if ((tramp_type == MONO_TRAMPOLINE_GENERIC) || (tramp_type == MONO_TRAMPOLINE_JUMP))
183                 ARM_STR_IMM (buf, ARMREG_V2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, method));
184         ARM_STR_IMM (buf, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, ebp));
185         /* save the IP (caller ip) */
186         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
187                 ARM_MOV_REG_IMM8 (buf, ARMREG_R2, 0);
188         } else {
189                 /* assumes STACK == sizeof (MonoLMF) */
190                 ARM_LDR_IMM (buf, ARMREG_R2, ARMREG_SP, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
191         }
192         ARM_STR_IMM (buf, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
193
194         /*
195          * Now we're ready to call xxx_trampoline ().
196          */
197         /* Arg 1: the saved registers. It was put in v1 */
198         ARM_MOV_REG_REG (buf, ARMREG_R0, ARMREG_V1);
199
200         /* Arg 2: code (next address to the instruction that called us) */
201         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
202                 ARM_MOV_REG_IMM8 (buf, ARMREG_R1, 0);
203         } else {
204                 ARM_MOV_REG_REG (buf, ARMREG_R1, ARMREG_V3);
205         }
206         
207         /* Arg 3: the specific argument, stored in v2
208          */
209         ARM_MOV_REG_REG (buf, ARMREG_R2, ARMREG_V2);
210
211         load_trampoline = buf;
212         buf += 4;
213
214         ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
215         ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_IP);
216         
217         /* OK, code address is now on r0. Move it to the place on the stack
218          * where IP was saved (it is now no more useful to us and it can be
219          * clobbered). This way we can just restore all the regs in one inst
220          * and branch to IP.
221          */
222         ARM_STR_IMM (buf, ARMREG_R0, ARMREG_V1, (ARMREG_R12 * 4));
223
224         /*
225          * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
226          * and the rest of the registers, so the method called will see
227          * the same state as before we executed.
228          * The pointer to MonoLMF is in r2.
229          */
230         ARM_MOV_REG_REG (buf, ARMREG_R2, ARMREG_SP);
231         /* ip = previous_lmf */
232         ARM_LDR_IMM (buf, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
233         /* lr = lmf_addr */
234         ARM_LDR_IMM (buf, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
235         /* *(lmf_addr) = previous_lmf */
236         ARM_STR_IMM (buf, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
237
238         /* Non-standard function epilogue. Instead of doing a proper
239          * return, we just jump to the compiled code.
240          */
241         /* Restore the registers and jump to the code:
242          * Note that IP has been conveniently set to the method addr.
243          */
244         ARM_ADD_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
245         ARM_POP_NWB (buf, 0x5fff);
246         /* do we need to set sp? */
247         ARM_ADD_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, (14 * 4));
248         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
249                 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_LR);
250         else
251                 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_IP);
252
253         constants = (gpointer*)buf;
254         constants [0] = mono_get_lmf_addr;
255         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
256                 constants [1] = mono_class_init_trampoline;
257         else if (tramp_type == MONO_TRAMPOLINE_AOT)
258                 constants [1] = mono_aot_trampoline;
259         else if (tramp_type == MONO_TRAMPOLINE_AOT_PLT)
260                 constants [1] = mono_aot_plt_trampoline;
261         else if (tramp_type == MONO_TRAMPOLINE_DELEGATE)
262                 constants [1] = mono_delegate_trampoline;
263         else
264                 constants [1] = mono_magic_trampoline;
265
266         /* backpatch by emitting the missing instructions skipped above */
267         ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (buf - load_get_lmf_addr - 8));
268         ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (buf + 4 - load_trampoline - 8));
269
270         buf += 8;
271
272         /* Flush instruction cache, since we've generated code */
273         mono_arch_flush_icache (code, buf - code);
274
275         /* Sanity check */
276         g_assert ((buf - code) <= GEN_TRAMP_SIZE);
277
278         return code;
279 }
280
281 #define SPEC_TRAMP_SIZE 24
282
283 gpointer
284 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
285 {
286         guint8 *code, *buf, *tramp;
287         gpointer *constants;
288         guint32 short_branch, size = SPEC_TRAMP_SIZE;
289
290         tramp = mono_get_trampoline_code (tramp_type);
291
292         mono_domain_lock (domain);
293         code = buf = mono_code_manager_reserve_align (domain->code_mp, size, 4);
294         if ((short_branch = branch_for_target_reachable (code + 8, tramp))) {
295                 size = 12;
296                 mono_code_manager_commit (domain->code_mp, code, SPEC_TRAMP_SIZE, size);
297         }
298         mono_domain_unlock (domain);
299
300         /* we could reduce this to 12 bytes if tramp is within reach:
301          * ARM_PUSH ()
302          * ARM_BL ()
303          * method-literal
304          * The called code can access method using the lr register
305          * A 20 byte sequence could be:
306          * ARM_PUSH ()
307          * ARM_MOV_REG_REG (lr, pc)
308          * ARM_LDR_IMM (pc, pc, 0)
309          * method-literal
310          * tramp-literal
311          */
312         /* We save all the registers, except PC and SP */
313         ARM_PUSH (buf, 0x5fff);
314         if (short_branch) {
315                 constants = (gpointer*)buf;
316                 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
317                 constants [1] = arg1;
318                 buf += 8;
319         } else {
320                 ARM_LDR_IMM (buf, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
321                 ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
322                 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_R1);
323
324                 constants = (gpointer*)buf;
325                 constants [0] = arg1;
326                 constants [1] = tramp;
327                 buf += 8;
328         }
329
330         /* Flush instruction cache, since we've generated code */
331         mono_arch_flush_icache (code, buf - code);
332
333         g_assert ((buf - code) <= size);
334
335         if (code_len)
336                 *code_len = buf - code;
337
338         return code;
339 }
340
341 /*
342  * This method is only called when running in the Mono Debugger.
343  */
344 gpointer
345 mono_debugger_create_notification_function (void)
346 {
347         guint8 *ptr, *buf;
348
349         ptr = buf = mono_global_codeman_reserve (8);
350         //FIXME: ARM_SWI (buf, 0x9F0001);
351         ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_LR);
352         mono_arch_flush_icache (ptr, buf - ptr);
353
354         return ptr;
355 }
356