2008-07-10 Zoltan Varga <vargaz@gmail.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  * @gsctx: the generic sharing context
44  * @m: method pointer
45  * @addr: pointer to native code for @m
46  *
47  * when value type methods are called through the vtable we need to unbox the
48  * this argument. This method returns a pointer to a trampoline which does
49  * unboxing before calling the method
50  */
51 gpointer
52 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
53 {
54         guint8 *code, *start;
55         int this_pos = 0;
56         MonoDomain *domain = mono_domain_get ();
57
58         if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
59                 this_pos = 1;
60
61         mono_domain_lock (domain);
62         start = code = mono_code_manager_reserve (domain->code_mp, 16);
63         mono_domain_unlock (domain);
64
65         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
66         ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
67         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
68         *(guint32*)code = (guint32)addr;
69         code += 4;
70         mono_arch_flush_icache (start, code - start);
71         g_assert ((code - start) <= 16);
72         /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
73         g_print ("unbox code is at %p for method at %p\n", start, addr);*/
74
75         return start;
76 }
77
78 void
79 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
80 {
81         guint32 *code = (guint32*)code_ptr;
82
83         /* This is the 'bl' or the 'mov pc' instruction */
84         --code;
85         
86         /*
87          * Note that methods are called also with the bl opcode.
88          */
89         if ((((*code) >> 25)  & 7) == 5) {
90                 /*g_print ("direct patching\n");*/
91                 arm_patch ((guint8*)code, addr);
92                 mono_arch_flush_icache ((guint8*)code, 4);
93                 return;
94         }
95
96         if ((((*code) >> 20) & 0xFF) == 0x12) {
97                 /*g_print ("patching bx\n");*/
98                 arm_patch ((guint8*)code, addr);
99                 mono_arch_flush_icache ((guint8*)(code - 2), 4);
100                 return;
101         }
102
103         g_assert_not_reached ();
104 }
105
106 void
107 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
108 {
109         guint32 ins = branch_for_target_reachable (code, addr);
110
111         if (ins)
112                 /* Patch the branch */
113                 ((guint32*)code) [0] = ins;
114         else
115                 /* Patch the jump address */
116                 ((guint32*)code) [1] = (guint32)addr;
117         mono_arch_flush_icache ((guint8*)code, 4);
118 }
119
120 void
121 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
122 {
123         return;
124 }
125
126 void
127 mono_arch_nullify_plt_entry (guint8 *code)
128 {
129         guint8 buf [4];
130         guint8 *p;
131
132         p = buf;
133         ARM_MOV_REG_REG (p, ARMREG_PC, ARMREG_LR);
134
135         ((guint32*)code) [0] = ((guint32*)buf) [0];
136         mono_arch_flush_icache ((guint8*)code, 4);
137 }
138
139
140 /* Stack size for trampoline function 
141  */
142 #define STACK (sizeof (MonoLMF))
143
144 /* Method-specific trampoline code fragment size */
145 #define METHOD_TRAMPOLINE_SIZE 64
146
147 /* Jump-specific trampoline code fragment size */
148 #define JUMP_TRAMPOLINE_SIZE   64
149
150 #define GEN_TRAMP_SIZE 192
151
152 /*
153  * Stack frame description when the generic trampoline is called.
154  * caller frame
155  * ------------------- old sp
156  *  MonoLMF
157  * ------------------- sp
158  */
159 guchar*
160 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
161 {
162         MonoJumpInfo *ji;
163         guint32 code_size;
164
165         return mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, FALSE);
166 }
167         
168 guchar*
169 mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
170 {
171         guint8 *buf, *code = NULL;
172         guint8 *load_get_lmf_addr, *load_trampoline;
173         gpointer *constants;
174
175         *ji = NULL;
176
177         /* Now we'll create in 'buf' the ARM trampoline code. This
178          is the trampoline code common to all methods  */
179         
180         code = buf = mono_global_codeman_reserve (GEN_TRAMP_SIZE);
181
182         /*
183          * At this point lr points to the specific arg and sp points to the saved
184          * regs on the stack (all but PC and SP). The original LR value has been
185          * saved as sp + LR_OFFSET by the push in the specific trampoline
186          */
187 #define LR_OFFSET (sizeof (gpointer) * 13)
188         ARM_MOV_REG_REG (buf, ARMREG_V1, ARMREG_SP);
189         ARM_LDR_IMM (buf, ARMREG_V2, ARMREG_LR, 0);
190         ARM_LDR_IMM (buf, ARMREG_V3, ARMREG_SP, LR_OFFSET);
191
192         /* ok, now we can continue with the MonoLMF setup, mostly untouched 
193          * from emit_prolog in mini-arm.c
194          * This is a sinthetized call to mono_get_lmf_addr ()
195          */
196         load_get_lmf_addr = buf;
197         buf += 4;
198         ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
199         ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_R0);
200
201         /* we build the MonoLMF structure on the stack - see mini-arm.h
202          * The pointer to the struct is put in r1.
203          * the iregs array is already allocated on the stack by push.
204          */
205         ARM_SUB_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
206         ARM_ADD_REG_IMM8 (buf, ARMREG_R1, ARMREG_SP, STACK - sizeof (MonoLMF));
207         /* r0 is the result from mono_get_lmf_addr () */
208         ARM_STR_IMM (buf, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
209         /* new_lmf->previous_lmf = *lmf_addr */
210         ARM_LDR_IMM (buf, ARMREG_R2, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
211         ARM_STR_IMM (buf, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
212         /* *(lmf_addr) = r1 */
213         ARM_STR_IMM (buf, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
214         /* save method info (it's in v2) */
215         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
216                 ARM_STR_IMM (buf, ARMREG_V2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, method));
217         ARM_STR_IMM (buf, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, ebp));
218         /* save the IP (caller ip) */
219         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
220                 ARM_MOV_REG_IMM8 (buf, ARMREG_R2, 0);
221         } else {
222                 /* assumes STACK == sizeof (MonoLMF) */
223                 ARM_LDR_IMM (buf, ARMREG_R2, ARMREG_SP, (G_STRUCT_OFFSET (MonoLMF, iregs) + 13*4));
224         }
225         ARM_STR_IMM (buf, ARMREG_R2, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
226
227         /*
228          * Now we're ready to call xxx_trampoline ().
229          */
230         /* Arg 1: the saved registers. It was put in v1 */
231         ARM_MOV_REG_REG (buf, ARMREG_R0, ARMREG_V1);
232
233         /* Arg 2: code (next address to the instruction that called us) */
234         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
235                 ARM_MOV_REG_IMM8 (buf, ARMREG_R1, 0);
236         } else {
237                 ARM_MOV_REG_REG (buf, ARMREG_R1, ARMREG_V3);
238         }
239         
240         /* Arg 3: the specific argument, stored in v2
241          */
242         ARM_MOV_REG_REG (buf, ARMREG_R2, ARMREG_V2);
243
244         load_trampoline = buf;
245         buf += 4;
246
247         ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
248         ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_IP);
249         
250         /* OK, code address is now on r0. Move it to the place on the stack
251          * where IP was saved (it is now no more useful to us and it can be
252          * clobbered). This way we can just restore all the regs in one inst
253          * and branch to IP.
254          */
255         ARM_STR_IMM (buf, ARMREG_R0, ARMREG_V1, (ARMREG_R12 * 4));
256
257         /* Check for thread interruption */
258         /* This is not perf critical code so no need to check the interrupt flag */
259         /* 
260          * Have to call the _force_ variant, since there could be a protected wrapper on the top of the stack.
261          */
262         ARM_LDR_IMM (buf, ARMREG_IP, ARMREG_PC, 0);
263         ARM_B (buf, 0);
264         *(gpointer*)buf = mono_thread_force_interruption_checkpoint;
265         buf += 4;
266         ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
267         ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_IP);
268
269         /*
270          * Now we restore the MonoLMF (see emit_epilogue in mini-arm.c)
271          * and the rest of the registers, so the method called will see
272          * the same state as before we executed.
273          * The pointer to MonoLMF is in r2.
274          */
275         ARM_MOV_REG_REG (buf, ARMREG_R2, ARMREG_SP);
276         /* ip = previous_lmf */
277         ARM_LDR_IMM (buf, ARMREG_IP, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
278         /* lr = lmf_addr */
279         ARM_LDR_IMM (buf, ARMREG_LR, ARMREG_R2, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
280         /* *(lmf_addr) = previous_lmf */
281         ARM_STR_IMM (buf, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
282
283         /* Non-standard function epilogue. Instead of doing a proper
284          * return, we just jump to the compiled code.
285          */
286         /* Restore the registers and jump to the code:
287          * Note that IP has been conveniently set to the method addr.
288          */
289         ARM_ADD_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, sizeof (MonoLMF) - sizeof (guint) * 14);
290         ARM_POP_NWB (buf, 0x5fff);
291         /* do we need to set sp? */
292         ARM_ADD_REG_IMM8 (buf, ARMREG_SP, ARMREG_SP, (14 * 4));
293         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
294                 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_LR);
295         else
296                 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_IP);
297
298         constants = (gpointer*)buf;
299         constants [0] = mono_get_lmf_addr;
300         constants [1] = (gpointer)mono_get_trampoline_func (tramp_type);
301
302         /* backpatch by emitting the missing instructions skipped above */
303         ARM_LDR_IMM (load_get_lmf_addr, ARMREG_R0, ARMREG_PC, (buf - load_get_lmf_addr - 8));
304         ARM_LDR_IMM (load_trampoline, ARMREG_IP, ARMREG_PC, (buf + 4 - load_trampoline - 8));
305
306         buf += 8;
307
308         /* Flush instruction cache, since we've generated code */
309         mono_arch_flush_icache (code, buf - code);
310
311         /* Sanity check */
312         g_assert ((buf - code) <= GEN_TRAMP_SIZE);
313
314         return code;
315 }
316
317 gpointer
318 mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
319 {
320         guint8 *buf, *code;
321
322         code = buf = mono_global_codeman_reserve (16);
323
324         // FIXME:
325
326         mono_arch_flush_icache (buf, code - buf);
327
328         *code_len = code - buf;
329
330         return buf;
331 }
332
333 #define SPEC_TRAMP_SIZE 24
334
335 gpointer
336 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
337 {
338         guint8 *code, *buf, *tramp;
339         gpointer *constants;
340         guint32 short_branch, size = SPEC_TRAMP_SIZE;
341
342         tramp = mono_get_trampoline_code (tramp_type);
343
344         mono_domain_lock (domain);
345         code = buf = mono_code_manager_reserve_align (domain->code_mp, size, 4);
346         if ((short_branch = branch_for_target_reachable (code + 8, tramp))) {
347                 size = 12;
348                 mono_code_manager_commit (domain->code_mp, code, SPEC_TRAMP_SIZE, size);
349         }
350         mono_domain_unlock (domain);
351
352         /* we could reduce this to 12 bytes if tramp is within reach:
353          * ARM_PUSH ()
354          * ARM_BL ()
355          * method-literal
356          * The called code can access method using the lr register
357          * A 20 byte sequence could be:
358          * ARM_PUSH ()
359          * ARM_MOV_REG_REG (lr, pc)
360          * ARM_LDR_IMM (pc, pc, 0)
361          * method-literal
362          * tramp-literal
363          */
364         /* We save all the registers, except PC and SP */
365         ARM_PUSH (buf, 0x5fff);
366         if (short_branch) {
367                 constants = (gpointer*)buf;
368                 constants [0] = GUINT_TO_POINTER (short_branch | (1 << 24));
369                 constants [1] = arg1;
370                 buf += 8;
371         } else {
372                 ARM_LDR_IMM (buf, ARMREG_R1, ARMREG_PC, 8); /* temp reg */
373                 ARM_MOV_REG_REG (buf, ARMREG_LR, ARMREG_PC);
374                 ARM_MOV_REG_REG (buf, ARMREG_PC, ARMREG_R1);
375
376                 constants = (gpointer*)buf;
377                 constants [0] = arg1;
378                 constants [1] = tramp;
379                 buf += 8;
380         }
381
382         /* Flush instruction cache, since we've generated code */
383         mono_arch_flush_icache (code, buf - code);
384
385         g_assert ((buf - code) <= size);
386
387         if (code_len)
388                 *code_len = buf - code;
389
390         return code;
391 }
392
393 gpointer
394 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
395 {
396         /* FIXME: implement! */
397         g_assert_not_reached ();
398         return NULL;
399 }
400
401 guint32
402 mono_arch_get_rgctx_lazy_fetch_offset (gpointer *regs)
403 {
404         /* FIXME: implement! */
405         g_assert_not_reached ();
406         return 0;
407 }