2 * tramp-ppc.c: JIT trampoline code for PowerPC
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>
10 * (C) 2001 Ximian, Inc.
11 * (C) 2007-2008 Andreas Faerber
17 #include <mono/metadata/abi-details.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/marshal.h>
20 #include <mono/metadata/tabledefs.h>
21 #include <mono/arch/ppc/ppc-codegen.h>
27 /* Same as mono_create_ftnptr, but doesn't require a domain */
29 mono_ppc_create_ftnptr (guint8 *code)
31 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
32 MonoPPCFunctionDescriptor *ftnptr = mono_global_codeman_reserve (sizeof (MonoPPCFunctionDescriptor));
46 * Return the instruction to jump from code to target, 0 if not
47 * reachable with a single instruction
50 branch_for_target_reachable (guint8 *branch, guint8 *target)
52 gint diff = target - branch;
53 g_assert ((diff & 3) == 0);
56 return (18 << 26) | (diff);
58 /* diff between 0 and -33554432 */
59 if (diff >= -33554432)
60 return (18 << 26) | (diff & ~0xfc000000);
66 * get_unbox_trampoline:
68 * @addr: pointer to native code for @m
70 * when value type methods are called through the vtable we need to unbox the
71 * this argument. This method returns a pointer to a trampoline which does
72 * unboxing before calling the method
75 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
80 MonoDomain *domain = mono_domain_get ();
81 int size = MONO_PPC_32_64_CASE (20, 32) + PPC_FTNPTR_SIZE;
83 addr = mono_get_addr_from_ftnptr (addr);
85 mono_domain_lock (domain);
86 start = code = mono_domain_code_reserve (domain, size);
87 code = mono_ppc_create_pre_code_ftnptr (code);
88 short_branch = branch_for_target_reachable (code + 4, addr);
90 mono_domain_code_commit (domain, code, size, 8);
91 mono_domain_unlock (domain);
94 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
95 ppc_emit32 (code, short_branch);
97 ppc_load_ptr (code, ppc_r0, addr);
98 ppc_mtctr (code, ppc_r0);
99 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
100 ppc_bcctr (code, 20, 0);
102 mono_arch_flush_icache (start, code - start);
103 g_assert ((code - start) <= size);
104 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
105 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
111 * mono_arch_get_static_rgctx_trampoline:
113 * Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
116 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
118 guint8 *code, *start, *p;
119 guint8 imm_buf [128];
120 guint32 short_branch;
121 MonoDomain *domain = mono_domain_get ();
123 int size = MONO_PPC_32_64_CASE (24, (PPC_LOAD_SEQUENCE_LENGTH * 2) + 8) + PPC_FTNPTR_SIZE;
125 addr = mono_get_addr_from_ftnptr (addr);
127 /* Compute size of code needed to emit mrgctx */
129 ppc_load_ptr (p, MONO_ARCH_RGCTX_REG, mrgctx);
130 imm_size = p - imm_buf;
132 mono_domain_lock (domain);
133 start = code = mono_domain_code_reserve (domain, size);
134 code = mono_ppc_create_pre_code_ftnptr (code);
135 short_branch = branch_for_target_reachable (code + imm_size, addr);
137 mono_domain_code_commit (domain, code, size, imm_size + 4);
138 mono_domain_unlock (domain);
141 ppc_load_ptr (code, MONO_ARCH_RGCTX_REG, mrgctx);
142 ppc_emit32 (code, short_branch);
144 ppc_load_ptr (code, ppc_r0, addr);
145 ppc_mtctr (code, ppc_r0);
146 ppc_load_ptr (code, MONO_ARCH_RGCTX_REG, mrgctx);
147 ppc_bcctr (code, 20, 0);
149 mono_arch_flush_icache (start, code - start);
150 g_assert ((code - start) <= size);
156 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
158 guint32 *code = (guint32*)code_ptr;
160 addr = mono_get_addr_from_ftnptr (addr);
162 /* This is the 'blrl' instruction */
166 * Note that methods are called also with the bl opcode.
168 if (((*code) >> 26) == 18) {
169 /*g_print ("direct patching\n");*/
170 ppc_patch ((guint8*)code, addr);
171 mono_arch_flush_icache ((guint8*)code, 4);
176 g_assert (mono_ppc_is_direct_call_sequence (code));
178 ppc_patch ((guint8*)code, addr);
182 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
184 guint32 ins1, ins2, offset;
186 /* Patch the jump table entry used by the plt entry */
188 /* Should be a lis+ori */
189 ins1 = ((guint32*)code)[0];
190 g_assert (ins1 >> 26 == 15);
191 ins2 = ((guint32*)code)[1];
192 g_assert (ins2 >> 26 == 24);
193 offset = ((ins1 & 0xffff) << 16) | (ins2 & 0xffff);
195 /* Either got or regs is set */
197 got = (gpointer*)(gsize) regs [30];
198 *(guint8**)((guint8*)got + offset) = addr;
201 /* Stack size for trampoline function
202 * PPC_MINIMAL_STACK_SIZE + 16 (args + alignment to ppc_magic_trampoline)
203 * + MonoLMF + 14 fp regs + 13 gregs + alignment
205 #define STACK (((PPC_MINIMAL_STACK_SIZE + 4 * sizeof (mgreg_t) + sizeof (MonoLMF) + 14 * sizeof (double) + 31 * sizeof (mgreg_t)) + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
207 /* Method-specific trampoline code fragment size */
208 #define METHOD_TRAMPOLINE_SIZE 64
210 /* Jump-specific trampoline code fragment size */
211 #define JUMP_TRAMPOLINE_SIZE 64
213 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
214 #define PPC_TOC_REG ppc_r2
216 #define PPC_TOC_REG -1
220 * Stack frame description when the generic trampoline is called.
222 * --------------------
224 * -------------------
225 * Saved FP registers 0-13
226 * -------------------
227 * Saved general registers 0-30
228 * -------------------
229 * param area for 3 args to ppc_magic_trampoline
230 * -------------------
232 * -------------------
235 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
238 guint8 *buf, *code = NULL;
240 gconstpointer tramp_handler;
241 int size = MONO_PPC_32_64_CASE (600, 800);
242 GSList *unwind_ops = NULL;
243 MonoJumpInfo *ji = NULL;
245 /* Now we'll create in 'buf' the PowerPC trampoline code. This
246 is the trampoline code common to all methods */
248 code = buf = mono_global_codeman_reserve (size);
250 ppc_str_update (code, ppc_r1, -STACK, ppc_r1);
252 /* start building the MonoLMF on the stack */
253 offset = STACK - sizeof (double) * MONO_SAVED_FREGS;
254 for (i = 14; i < 32; i++) {
255 ppc_stfd (code, i, offset, ppc_r1);
256 offset += sizeof (double);
259 * now the integer registers.
261 offset = STACK - sizeof (MonoLMF) + G_STRUCT_OFFSET (MonoLMF, iregs);
262 ppc_str_multiple (code, ppc_r13, offset, ppc_r1);
264 /* Now save the rest of the registers below the MonoLMF struct, first 14
265 * fp regs and then the 31 gregs.
267 offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
268 for (i = 0; i < 14; i++) {
269 ppc_stfd (code, i, offset, ppc_r1);
270 offset += sizeof (double);
272 #define GREGS_OFFSET (STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (31 * sizeof (mgreg_t)))
273 offset = GREGS_OFFSET;
274 for (i = 0; i < 31; i++) {
275 ppc_str (code, i, offset, ppc_r1);
276 offset += sizeof (mgreg_t);
279 /* we got here through a jump to the ctr reg, we must save the lr
280 * in the parent frame (we do it here to reduce the size of the
281 * method-specific trampoline)
283 ppc_mflr (code, ppc_r0);
284 ppc_str (code, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
286 /* ok, now we can continue with the MonoLMF setup, mostly untouched
287 * from emit_prolog in mini-ppc.c
290 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
291 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
292 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
293 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
295 ppc_mtlr (code, ppc_r12);
298 ppc_load_func (code, PPC_CALL_REG, mono_get_lmf_addr);
299 ppc_mtlr (code, PPC_CALL_REG);
302 /* we build the MonoLMF structure on the stack - see mini-ppc.h
303 * The pointer to the struct is put in ppc_r12.
305 ppc_addi (code, ppc_r12, ppc_sp, STACK - sizeof (MonoLMF));
306 ppc_stptr (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r12);
307 /* new_lmf->previous_lmf = *lmf_addr */
308 ppc_ldptr (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
309 ppc_stptr (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r12);
310 /* *(lmf_addr) = r12 */
311 ppc_stptr (code, ppc_r12, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
312 /* save method info (it's stored on the stack, so get it first). */
313 if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP)) {
314 ppc_ldr (code, ppc_r0, GREGS_OFFSET, ppc_r1);
315 ppc_stptr (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r12);
317 ppc_load (code, ppc_r0, 0);
318 ppc_stptr (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r12);
320 /* store the frame pointer of the calling method */
321 ppc_addi (code, ppc_r0, ppc_sp, STACK);
322 ppc_stptr (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r12);
323 /* save the IP (caller ip) */
324 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
325 ppc_li (code, ppc_r0, 0);
327 ppc_ldr (code, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
329 ppc_stptr (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r12);
332 * Now we're ready to call trampoline (mgreg_t *regs, guint8 *code, gpointer value, guint8 *tramp)
333 * Note that the last argument is unused.
335 /* Arg 1: a pointer to the registers */
336 ppc_addi (code, ppc_r3, ppc_r1, GREGS_OFFSET);
338 /* Arg 2: code (next address to the instruction that called us) */
339 if (tramp_type == MONO_TRAMPOLINE_JUMP)
340 ppc_li (code, ppc_r4, 0);
342 ppc_ldr (code, ppc_r4, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
344 /* Arg 3: trampoline argument */
345 ppc_ldr (code, ppc_r5, GREGS_OFFSET, ppc_r1);
348 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("trampoline_func_%d", tramp_type));
349 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
350 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
351 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
353 ppc_mtlr (code, ppc_r12);
356 tramp_handler = mono_get_trampoline_func (tramp_type);
357 ppc_load_func (code, PPC_CALL_REG, tramp_handler);
358 ppc_mtlr (code, PPC_CALL_REG);
362 /* OK, code address is now on r3. Move it to the counter reg
363 * so it will be ready for the final jump: this is safe since we
364 * won't do any more calls.
366 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
367 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
368 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r3);
369 ppc_ldptr (code, ppc_r3, 0, ppc_r3);
371 ppc_mtctr (code, ppc_r3);
375 * Now we restore the MonoLMF (see emit_epilogue in mini-ppc.c)
376 * and the rest of the registers, so the method called will see
377 * the same state as before we executed.
378 * The pointer to MonoLMF is in ppc_r12.
380 ppc_addi (code, ppc_r12, ppc_r1, STACK - sizeof (MonoLMF));
381 /* r5 = previous_lmf */
382 ppc_ldptr (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r12);
384 ppc_ldptr (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r12);
385 /* *(lmf_addr) = previous_lmf */
386 ppc_stptr (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
388 ppc_ldr_multiple (code, ppc_r13, G_STRUCT_OFFSET(MonoLMF, iregs), ppc_r12);
390 for (i = 14; i < 32; i++)
391 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r12);
393 /* restore the volatile registers, we skip r1, of course */
394 offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
395 for (i = 0; i < 14; i++) {
396 ppc_lfd (code, i, offset, ppc_r1);
397 offset += sizeof (double);
399 offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (31 * sizeof (mgreg_t));
400 ppc_ldr (code, ppc_r0, offset, ppc_r1);
401 offset += 2 * sizeof (mgreg_t);
402 for (i = 2; i < 13; i++) {
403 if (i != PPC_TOC_REG && (i != 3 || tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
404 ppc_ldr (code, i, offset, ppc_r1);
405 offset += sizeof (mgreg_t);
408 /* Non-standard function epilogue. Instead of doing a proper
409 * return, we just jump to the compiled code.
411 /* Restore stack pointer and LR and jump to the code */
412 ppc_ldr (code, ppc_r1, 0, ppc_r1);
413 ppc_ldr (code, ppc_r12, PPC_RET_ADDR_OFFSET, ppc_r1);
414 ppc_mtlr (code, ppc_r12);
415 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
418 ppc_bcctr (code, 20, 0);
420 /* Flush instruction cache, since we've generated code */
421 mono_arch_flush_icache (buf, code - buf);
424 g_assert ((code - buf) <= size);
427 tramp_name = mono_get_generic_trampoline_name (tramp_type);
428 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
434 #define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
436 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
438 guint8 *code, *buf, *tramp;
439 guint32 short_branch;
441 tramp = mono_get_trampoline_code (tramp_type);
443 mono_domain_lock (domain);
444 code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
445 short_branch = branch_for_target_reachable (code + MONO_PPC_32_64_CASE (8, 5*4), tramp);
446 #ifdef __mono_ppc64__
447 /* FIXME: make shorter if possible */
450 mono_domain_code_commit (domain, code, TRAMPOLINE_SIZE, 12);
452 mono_domain_unlock (domain);
455 ppc_load_sequence (code, ppc_r0, (mgreg_t)(gsize) arg1);
456 ppc_emit32 (code, short_branch);
458 /* Prepare the jump to the generic trampoline code.*/
459 ppc_load_ptr (code, ppc_r0, tramp);
460 ppc_mtctr (code, ppc_r0);
462 /* And finally put 'arg1' in r0 and fly! */
463 ppc_load_ptr (code, ppc_r0, arg1);
464 ppc_bcctr (code, 20, 0);
467 /* Flush instruction cache, since we've generated code */
468 mono_arch_flush_icache (buf, code - buf);
470 g_assert ((code - buf) <= TRAMPOLINE_SIZE);
473 *code_len = code - buf;
479 emit_trampoline_jump (guint8 *code, guint8 *tramp)
481 guint32 short_branch = branch_for_target_reachable (code, tramp);
483 /* FIXME: we can save a few bytes here by committing if the
484 short branch is possible */
486 ppc_emit32 (code, short_branch);
488 ppc_load_ptr (code, ppc_r0, tramp);
489 ppc_mtctr (code, ppc_r0);
490 ppc_bcctr (code, 20, 0);
497 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
501 guint8 **rgctx_null_jumps;
506 MonoJumpInfo *ji = NULL;
507 GSList *unwind_ops = NULL;
509 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
510 index = MONO_RGCTX_SLOT_INDEX (slot);
512 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
513 for (depth = 0; ; ++depth) {
514 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
516 if (index < size - 1)
521 tramp_size = MONO_PPC_32_64_CASE (40, 52) + 12 * depth;
529 code = buf = mono_global_codeman_reserve (tramp_size);
531 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
535 ppc_mr (code, ppc_r4, PPC_FIRST_ARG_REG);
537 /* load rgctx ptr from vtable */
538 ppc_ldptr (code, ppc_r4, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), PPC_FIRST_ARG_REG);
539 /* is the rgctx ptr null? */
540 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
541 /* if yes, jump to actual trampoline */
542 rgctx_null_jumps [0] = code;
543 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
546 for (i = 0; i < depth; ++i) {
547 /* load ptr to next array */
548 if (mrgctx && i == 0)
549 ppc_ldptr (code, ppc_r4, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, ppc_r4);
551 ppc_ldptr (code, ppc_r4, 0, ppc_r4);
552 /* is the ptr null? */
553 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
554 /* if yes, jump to actual trampoline */
555 rgctx_null_jumps [i + 1] = code;
556 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
560 ppc_ldptr (code, ppc_r4, sizeof (gpointer) * (index + 1), ppc_r4);
561 /* is the slot null? */
562 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
563 /* if yes, jump to actual trampoline */
564 rgctx_null_jumps [depth + 1] = code;
565 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
566 /* otherwise return r4 */
567 /* FIXME: if we use r3 as the work register we can avoid this copy */
568 ppc_mr (code, ppc_r3, ppc_r4);
571 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
572 ppc_patch (rgctx_null_jumps [i], code);
574 g_free (rgctx_null_jumps);
576 /* move the rgctx pointer to the VTABLE register */
577 ppc_mr (code, MONO_ARCH_VTABLE_REG, ppc_r3);
580 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
581 /* Branch to the trampoline */
582 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
583 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
585 ppc_mtctr (code, ppc_r12);
586 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
588 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
589 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
591 /* jump to the actual trampoline */
592 code = emit_trampoline_jump (code, tramp);
595 mono_arch_flush_icache (buf, code - buf);
597 g_assert (code - buf <= tramp_size);
599 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
600 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
607 mono_arch_get_call_target (guint8 *code)
610 guint32 ins = ((guint32*)(gpointer)code) [-1];
612 if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
613 gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
614 guint8 *target = code - 4 + (disp * 4);
623 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
625 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
626 return ((guint32*)plt_entry) [8];
628 return ((guint32*)plt_entry) [6];