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