New test.
[mono.git] / mono / mini / tramp-ppc.c
1 /*
2  * tramp-ppc.c: JIT trampoline code for PowerPC
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *   Carlos Valiente <yo@virutass.net>
8  *   Andreas Faerber <andreas.faerber@web.de>
9  *
10  * (C) 2001 Ximian, Inc.
11  * (C) 2007-2008 Andreas Faerber
12  */
13
14 #include <config.h>
15 #include <glib.h>
16
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/marshal.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/arch/ppc/ppc-codegen.h>
21
22 #include "mini.h"
23 #include "mini-ppc.h"
24
25 /*
26  * Return the instruction to jump from code to target, 0 if not
27  * reachable with a single instruction
28  */
29 static guint32
30 branch_for_target_reachable (guint8 *branch, guint8 *target)
31 {
32         gint diff = target - branch;
33         g_assert ((diff & 3) == 0);
34         if (diff >= 0) {
35                 if (diff <= 33554431)
36                         return (18 << 26) | (diff);
37         } else {
38                 /* diff between 0 and -33554432 */
39                 if (diff >= -33554432)
40                         return (18 << 26) | (diff & ~0xfc000000);
41         }
42         return 0;
43 }
44
45 /*
46  * get_unbox_trampoline:
47  * @gsctx: the generic sharing context
48  * @m: method pointer
49  * @addr: pointer to native code for @m
50  *
51  * when value type methods are called through the vtable we need to unbox the
52  * this argument. This method returns a pointer to a trampoline which does
53  * unboxing before calling the method
54  */
55 gpointer
56 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
57 {
58         guint8 *code, *start;
59         int this_pos = 3;
60         guint32 short_branch;
61         MonoDomain *domain = mono_domain_get ();
62         int size = MONO_PPC_32_64_CASE (20, 32) + PPC_FTNPTR_SIZE;
63
64         addr = mono_get_addr_from_ftnptr (addr);
65
66         if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
67                 this_pos = 4;
68             
69         mono_domain_lock (domain);
70         start = code = mono_code_manager_reserve (domain->code_mp, size);
71         code = mono_ppc_create_pre_code_ftnptr (code);
72         short_branch = branch_for_target_reachable (code + 4, addr);
73         if (short_branch)
74                 mono_code_manager_commit (domain->code_mp, code, size, 8);
75         mono_domain_unlock (domain);
76
77         if (short_branch) {
78                 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
79                 ppc_emit32 (code, short_branch);
80         } else {
81                 ppc_load (code, ppc_r0, addr);
82                 ppc_mtctr (code, ppc_r0);
83                 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
84                 ppc_bcctr (code, 20, 0);
85         }
86         mono_arch_flush_icache (start, code - start);
87         g_assert ((code - start) <= size);
88         /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
89         g_print ("unbox code is at %p for method at %p\n", start, addr);*/
90
91         return start;
92 }
93
94 void
95 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
96 {
97         guint32 *code = (guint32*)code_ptr;
98
99         addr = mono_get_addr_from_ftnptr (addr);
100
101         /* This is the 'blrl' instruction */
102         --code;
103         
104         /*
105          * Note that methods are called also with the bl opcode.
106          */
107         if (((*code) >> 26) == 18) {
108                 /*g_print ("direct patching\n");*/
109                 ppc_patch ((guint8*)code, addr);
110                 mono_arch_flush_icache ((guint8*)code, 4);
111                 return;
112         }
113         
114         /* Sanity check */
115         g_assert (mono_ppc_is_direct_call_sequence (code));
116
117         ppc_patch ((guint8*)code, addr);
118 }
119
120 void
121 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
122 {
123         g_assert_not_reached ();
124 }
125
126 void
127 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
128 {
129         return;
130 }
131
132 void
133 mono_arch_nullify_plt_entry (guint8 *code)
134 {
135         g_assert_not_reached ();
136 }
137
138 /* Stack size for trampoline function 
139  * PPC_MINIMAL_STACK_SIZE + 16 (args + alignment to ppc_magic_trampoline)
140  * + MonoLMF + 14 fp regs + 13 gregs + alignment
141  * #define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 13 * (sizeof (gulong)))
142  * STACK would be 444 for 32 bit darwin
143  */
144 #ifdef __mono_ppc64__
145 #define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 13 * sizeof (gulong))
146 #else
147 #define STACK (448)
148 #endif
149
150 /* Method-specific trampoline code fragment size */
151 #define METHOD_TRAMPOLINE_SIZE 64
152
153 /* Jump-specific trampoline code fragment size */
154 #define JUMP_TRAMPOLINE_SIZE   64
155
156 /*
157  * Stack frame description when the generic trampoline is called.
158  * caller frame
159  * --------------------
160  *  MonoLMF
161  *  -------------------
162  *  Saved FP registers 0-13
163  *  -------------------
164  *  Saved general registers 0-12
165  *  -------------------
166  *  param area for 3 args to ppc_magic_trampoline
167  *  -------------------
168  *  linkage area
169  *  -------------------
170  */
171 guchar*
172 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
173 {
174         guint8 *buf, *code = NULL;
175         int i, offset;
176         gconstpointer tramp_handler;
177         int size = MONO_PPC_32_64_CASE (512, 688);
178
179         /* Now we'll create in 'buf' the PowerPC trampoline code. This
180            is the trampoline code common to all methods  */
181
182         code = buf = mono_global_codeman_reserve (size);
183
184         ppc_store_reg_update (buf, ppc_r1, -STACK, ppc_r1);
185
186         /* start building the MonoLMF on the stack */
187         offset = STACK - sizeof (double) * MONO_SAVED_FREGS;
188         for (i = 14; i < 32; i++) {
189                 ppc_stfd (buf, i, offset, ppc_r1);
190                 offset += sizeof (double);
191         }
192         /* 
193          * now the integer registers.
194          */
195         offset = STACK - sizeof (MonoLMF) + G_STRUCT_OFFSET (MonoLMF, iregs);
196         ppc_store_multiple_regs (buf, ppc_r13, ppc_r1, offset);
197
198         /* Now save the rest of the registers below the MonoLMF struct, first 14
199          * fp regs and then the 13 gregs.
200          */
201         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
202         for (i = 0; i < 14; i++) {
203                 ppc_stfd (buf, i, offset, ppc_r1);
204                 offset += sizeof (double);
205         }
206 #define GREGS_OFFSET (STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (13 * sizeof (gulong)))
207         offset = GREGS_OFFSET;
208         for (i = 0; i < 13; i++) {
209                 ppc_store_reg (buf, i, offset, ppc_r1);
210                 offset += sizeof (gulong);
211         }
212         /* we got here through a jump to the ctr reg, we must save the lr
213          * in the parent frame (we do it here to reduce the size of the
214          * method-specific trampoline)
215          */
216         ppc_mflr (buf, ppc_r0);
217         ppc_store_reg (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
218
219         /* ok, now we can continue with the MonoLMF setup, mostly untouched 
220          * from emit_prolog in mini-ppc.c
221          */
222         ppc_load_func (buf, ppc_r0, mono_get_lmf_addr);
223         ppc_mtlr (buf, ppc_r0);
224         ppc_blrl (buf);
225         /* we build the MonoLMF structure on the stack - see mini-ppc.h
226          * The pointer to the struct is put in ppc_r11.
227          */
228         ppc_addi (buf, ppc_r11, ppc_sp, STACK - sizeof (MonoLMF));
229         ppc_store_reg (buf, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
230         /* new_lmf->previous_lmf = *lmf_addr */
231         ppc_load_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
232         ppc_store_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
233         /* *(lmf_addr) = r11 */
234         ppc_store_reg (buf, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
235         /* save method info (it's stored on the stack, so get it first and put it
236          * in r5 as it's the third argument to the function)
237          */
238         if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
239                 ppc_load_reg (buf, ppc_r5, GREGS_OFFSET + PPC_FIRST_ARG_REG * sizeof (gpointer), ppc_r1);
240         else
241                 ppc_load_reg (buf, ppc_r5, GREGS_OFFSET, ppc_r1);
242         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
243                 ppc_store_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
244         ppc_store_reg (buf, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
245         /* save the IP (caller ip) */
246         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
247                 ppc_li (buf, ppc_r0, 0);
248         } else {
249                 ppc_load_reg (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
250         }
251         ppc_store_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
252
253         /*
254          * Now we're ready to call trampoline (gssize *regs, guint8 *code, gpointer value, guint8 *tramp)
255          * Note that the last argument is unused.
256          */
257         /* Arg 1: a pointer to the registers */
258         ppc_addi (buf, ppc_r3, ppc_r1, GREGS_OFFSET);
259                 
260         /* Arg 2: code (next address to the instruction that called us) */
261         if (tramp_type == MONO_TRAMPOLINE_JUMP)
262                 ppc_li (buf, ppc_r4, 0);
263         else
264                 ppc_load_reg  (buf, ppc_r4, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
265
266         /* Arg 3: MonoMethod *method. It was put in r5 already above */
267         /*ppc_mr  (buf, ppc_r5, ppc_r5);*/
268
269         tramp_handler = mono_get_trampoline_func (tramp_type);
270         ppc_load_func (buf, ppc_r0, tramp_handler);
271         ppc_mtlr (buf, ppc_r0);
272         ppc_blrl (buf);
273                 
274         /* OK, code address is now on r3. Move it to the counter reg
275          * so it will be ready for the final jump: this is safe since we
276          * won't do any more calls.
277          */
278         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
279 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
280                 ppc_load_reg (buf, ppc_r3, 0, ppc_r3);
281 #endif
282                 ppc_mtctr (buf, ppc_r3);
283         }
284
285         /*
286          * Now we restore the MonoLMF (see emit_epilogue in mini-ppc.c)
287          * and the rest of the registers, so the method called will see
288          * the same state as before we executed.
289          * The pointer to MonoLMF is in ppc_r11.
290          */
291         ppc_addi (buf, ppc_r11, ppc_r1, STACK - sizeof (MonoLMF));
292         /* r5 = previous_lmf */
293         ppc_load_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
294         /* r6 = lmf_addr */
295         ppc_load_reg (buf, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
296         /* *(lmf_addr) = previous_lmf */
297         ppc_store_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
298         /* restore iregs */
299         ppc_load_multiple_regs (buf, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
300         /* restore fregs */
301         for (i = 14; i < 32; i++)
302                 ppc_lfd (buf, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
303
304         /* restore the volatile registers, we skip r1, of course */
305         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
306         for (i = 0; i < 14; i++) {
307                 ppc_lfd (buf, i, offset, ppc_r1);
308                 offset += sizeof (double);
309         }
310         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (13 * sizeof (gulong));
311         ppc_load_reg (buf, ppc_r0, offset, ppc_r1);
312         offset += 2 * sizeof (gulong);
313         for (i = 2; i < 13; i++) {
314                 if (i != 3 || tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
315                         ppc_load_reg (buf, i, offset, ppc_r1);
316                 offset += sizeof (gulong);
317         }
318
319         /* Non-standard function epilogue. Instead of doing a proper
320          * return, we just jump to the compiled code.
321          */
322         /* Restore stack pointer and LR and jump to the code */
323         ppc_load_reg  (buf, ppc_r1,  0, ppc_r1);
324         ppc_load_reg  (buf, ppc_r11, PPC_RET_ADDR_OFFSET, ppc_r1);
325         ppc_mtlr (buf, ppc_r11);
326         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
327                 ppc_blr (buf);
328         else
329                 ppc_bcctr (buf, 20, 0);
330
331         /* Flush instruction cache, since we've generated code */
332         mono_arch_flush_icache (code, buf - code);
333         
334         /* Sanity check */
335         g_assert ((buf - code) <= size);
336
337         return code;
338 }
339
340 #define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
341 gpointer
342 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
343 {
344         guint8 *code, *buf, *tramp;
345         guint32 short_branch;
346
347         tramp = mono_get_trampoline_code (tramp_type);
348
349         mono_domain_lock (domain);
350         code = buf = mono_code_manager_reserve_align (domain->code_mp, TRAMPOLINE_SIZE, 4);
351         short_branch = branch_for_target_reachable (code + MONO_PPC_32_64_CASE (8, 5*4), tramp);
352 #ifdef __mono_ppc64__
353         /* FIXME: make shorter if possible */
354 #else
355         if (short_branch)
356                 mono_code_manager_commit (domain->code_mp, code, TRAMPOLINE_SIZE, 12);
357 #endif
358         mono_domain_unlock (domain);
359
360         if (short_branch) {
361                 ppc_load_sequence (buf, ppc_r0, (gulong) arg1);
362                 ppc_emit32 (buf, short_branch);
363         } else {
364                 /* Prepare the jump to the generic trampoline code.*/
365                 ppc_load (buf, ppc_r0, (gulong) tramp);
366                 ppc_mtctr (buf, ppc_r0);
367         
368                 /* And finally put 'arg1' in r0 and fly! */
369                 ppc_load (buf, ppc_r0, (gulong) arg1);
370                 ppc_bcctr (buf, 20, 0);
371         }
372         
373         /* Flush instruction cache, since we've generated code */
374         mono_arch_flush_icache (code, buf - code);
375
376         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
377         if (code_len)
378                 *code_len = buf - code;
379
380         return code;
381 }
382
383 static guint8*
384 emit_trampoline_jump (guint8 *code, guint8 *tramp)
385 {
386         guint32 short_branch = branch_for_target_reachable (code, tramp);
387
388         /* FIXME: we can save a few bytes here by committing if the
389            short branch is possible */
390         if (short_branch) {
391                 ppc_emit32 (code, short_branch);
392         } else {
393                 ppc_load (code, ppc_r0, tramp);
394                 ppc_mtctr (code, ppc_r0);
395                 ppc_bcctr (code, 20, 0);
396         }
397
398         return code;
399 }
400
401 gpointer
402 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
403 {
404 #ifdef MONO_ARCH_VTABLE_REG
405         guint8 *tramp;
406         guint8 *code, *buf;
407         guint8 **rgctx_null_jumps;
408         int tramp_size;
409         int depth, index;
410         int i;
411         gboolean mrgctx;
412
413         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
414         index = MONO_RGCTX_SLOT_INDEX (slot);
415         if (mrgctx)
416                 index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
417         for (depth = 0; ; ++depth) {
418                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
419
420                 if (index < size - 1)
421                         break;
422                 index -= size - 1;
423         }
424
425         tramp_size = MONO_PPC_32_64_CASE (40, 52) + 12 * depth;
426         if (mrgctx)
427                 tramp_size += 4;
428         else
429                 tramp_size += 12;
430
431         code = buf = mono_global_codeman_reserve (tramp_size);
432
433         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
434
435         if (mrgctx) {
436                 /* get mrgctx ptr */
437                 ppc_mr (code, ppc_r4, PPC_FIRST_ARG_REG);
438         } else {
439                 /* load rgctx ptr from vtable */
440                 ppc_load_reg (code, ppc_r4, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), PPC_FIRST_ARG_REG);
441                 /* is the rgctx ptr null? */
442                 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
443                 /* if yes, jump to actual trampoline */
444                 rgctx_null_jumps [0] = code;
445                 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
446         }
447
448         for (i = 0; i < depth; ++i) {
449                 /* load ptr to next array */
450                 if (mrgctx && i == 0)
451                         ppc_load_reg (code, ppc_r4, sizeof (MonoMethodRuntimeGenericContext), ppc_r4);
452                 else
453                         ppc_load_reg (code, ppc_r4, 0, ppc_r4);
454                 /* is the ptr null? */
455                 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
456                 /* if yes, jump to actual trampoline */
457                 rgctx_null_jumps [i + 1] = code;
458                 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
459         }
460
461         /* fetch slot */
462         ppc_load_reg (code, ppc_r4, sizeof (gpointer) * (index  + 1), ppc_r4);
463         /* is the slot null? */
464         ppc_compare_reg_imm (code, 0, ppc_r4, 0);
465         /* if yes, jump to actual trampoline */
466         rgctx_null_jumps [depth + 1] = code;
467         ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
468         /* otherwise return r4 */
469         /* FIXME: if we use r3 as the work register we can avoid this copy */
470         ppc_mr (code, ppc_r3, ppc_r4);
471         ppc_blr (code);
472
473         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
474                 ppc_patch (rgctx_null_jumps [i], code);
475
476         g_free (rgctx_null_jumps);
477
478         /* move the rgctx pointer to the VTABLE register */
479         ppc_mr (code, MONO_ARCH_VTABLE_REG, ppc_r3);
480
481         tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
482                         MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
483
484         /* jump to the actual trampoline */
485         code = emit_trampoline_jump (code, tramp);
486
487         mono_arch_flush_icache (buf, code - buf);
488
489         g_assert (code - buf <= tramp_size);
490
491         return buf;
492 #else
493         g_assert_not_reached ();
494 #endif
495 }
496
497 gpointer
498 mono_arch_create_generic_class_init_trampoline (void)
499 {
500         guint8 *tramp;
501         guint8 *code, *buf;
502         static int byte_offset = -1;
503         static guint8 bitmask;
504         guint8 *jump;
505         int tramp_size;
506
507         tramp_size = 32;
508
509         code = buf = mono_global_codeman_reserve (tramp_size);
510
511         if (byte_offset < 0)
512                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
513
514         ppc_lbz (code, ppc_r4, byte_offset, PPC_FIRST_ARG_REG);
515         ppc_andid (code, ppc_r4, ppc_r4, bitmask);
516         jump = code;
517         ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
518
519         ppc_blr (code);
520
521         ppc_patch (jump, code);
522
523         tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
524                         mono_get_root_domain (), NULL);
525
526         /* jump to the actual trampoline */
527         code = emit_trampoline_jump (code, tramp);
528
529         mono_arch_flush_icache (buf, code - buf);
530
531         g_assert (code - buf <= tramp_size);
532
533         return buf;
534 }