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/appdomain.h>
18 #include <mono/metadata/marshal.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/arch/ppc/ppc-codegen.h>
26 * Return the instruction to jump from code to target, 0 if not
27 * reachable with a single instruction
30 branch_for_target_reachable (guint8 *branch, guint8 *target)
32 gint diff = target - branch;
33 g_assert ((diff & 3) == 0);
36 return (18 << 26) | (diff);
38 /* diff between 0 and -33554432 */
39 if (diff >= -33554432)
40 return (18 << 26) | (diff & ~0xfc000000);
46 * get_unbox_trampoline:
47 * @gsctx: the generic sharing context
49 * @addr: pointer to native code for @m
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
56 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
61 MonoDomain *domain = mono_domain_get ();
62 int size = MONO_PPC_32_64_CASE (20, 32) + PPC_FTNPTR_SIZE;
64 addr = mono_get_addr_from_ftnptr (addr);
66 if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
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);
74 mono_code_manager_commit (domain->code_mp, code, size, 8);
75 mono_domain_unlock (domain);
78 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
79 ppc_emit32 (code, short_branch);
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);
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);*/
95 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
97 guint32 *code = (guint32*)code_ptr;
99 addr = mono_get_addr_from_ftnptr (addr);
101 /* This is the 'blrl' instruction */
105 * Note that methods are called also with the bl opcode.
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);
115 g_assert (mono_ppc_is_direct_call_sequence (code));
117 ppc_patch ((guint8*)code, addr);
121 mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
123 g_assert_not_reached ();
127 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
133 mono_arch_nullify_plt_entry (guint8 *code)
135 g_assert_not_reached ();
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
144 #ifdef __mono_ppc64__
145 #define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 13 * sizeof (gulong))
150 /* Method-specific trampoline code fragment size */
151 #define METHOD_TRAMPOLINE_SIZE 64
153 /* Jump-specific trampoline code fragment size */
154 #define JUMP_TRAMPOLINE_SIZE 64
157 * Stack frame description when the generic trampoline is called.
159 * --------------------
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 * -------------------
169 * -------------------
172 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
174 guint8 *buf, *code = NULL;
176 gconstpointer tramp_handler;
177 int size = MONO_PPC_32_64_CASE (512, 688);
179 /* Now we'll create in 'buf' the PowerPC trampoline code. This
180 is the trampoline code common to all methods */
182 code = buf = mono_global_codeman_reserve (size);
184 ppc_store_reg_update (buf, ppc_r1, -STACK, ppc_r1);
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);
193 * now the integer registers.
195 offset = STACK - sizeof (MonoLMF) + G_STRUCT_OFFSET (MonoLMF, iregs);
196 ppc_store_multiple_regs (buf, ppc_r13, ppc_r1, offset);
198 /* Now save the rest of the registers below the MonoLMF struct, first 14
199 * fp regs and then the 13 gregs.
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);
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);
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)
216 ppc_mflr (buf, ppc_r0);
217 ppc_store_reg (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
219 /* ok, now we can continue with the MonoLMF setup, mostly untouched
220 * from emit_prolog in mini-ppc.c
222 ppc_load_func (buf, ppc_r0, mono_get_lmf_addr);
223 ppc_mtlr (buf, ppc_r0);
225 /* we build the MonoLMF structure on the stack - see mini-ppc.h
226 * The pointer to the struct is put in ppc_r11.
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)
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);
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);
249 ppc_load_reg (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
251 ppc_store_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
254 * Now we're ready to call trampoline (gssize *regs, guint8 *code, gpointer value, guint8 *tramp)
255 * Note that the last argument is unused.
257 /* Arg 1: a pointer to the registers */
258 ppc_addi (buf, ppc_r3, ppc_r1, GREGS_OFFSET);
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);
264 ppc_load_reg (buf, ppc_r4, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
266 /* Arg 3: MonoMethod *method. It was put in r5 already above */
267 /*ppc_mr (buf, ppc_r5, ppc_r5);*/
269 tramp_handler = mono_get_trampoline_func (tramp_type);
270 ppc_load_func (buf, ppc_r0, tramp_handler);
271 ppc_mtlr (buf, ppc_r0);
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.
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);
282 ppc_mtctr (buf, ppc_r3);
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.
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);
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);
299 ppc_load_multiple_regs (buf, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
301 for (i = 14; i < 32; i++)
302 ppc_lfd (buf, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
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);
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);
319 /* Non-standard function epilogue. Instead of doing a proper
320 * return, we just jump to the compiled code.
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))
329 ppc_bcctr (buf, 20, 0);
331 /* Flush instruction cache, since we've generated code */
332 mono_arch_flush_icache (code, buf - code);
335 g_assert ((buf - code) <= size);
340 #define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
342 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
344 guint8 *code, *buf, *tramp;
345 guint32 short_branch;
347 tramp = mono_get_trampoline_code (tramp_type);
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 */
356 mono_code_manager_commit (domain->code_mp, code, TRAMPOLINE_SIZE, 12);
358 mono_domain_unlock (domain);
361 ppc_load_sequence (buf, ppc_r0, (gulong) arg1);
362 ppc_emit32 (buf, short_branch);
364 /* Prepare the jump to the generic trampoline code.*/
365 ppc_load (buf, ppc_r0, (gulong) tramp);
366 ppc_mtctr (buf, ppc_r0);
368 /* And finally put 'arg1' in r0 and fly! */
369 ppc_load (buf, ppc_r0, (gulong) arg1);
370 ppc_bcctr (buf, 20, 0);
373 /* Flush instruction cache, since we've generated code */
374 mono_arch_flush_icache (code, buf - code);
376 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
378 *code_len = buf - code;
384 emit_trampoline_jump (guint8 *code, guint8 *tramp)
386 guint32 short_branch = branch_for_target_reachable (code, tramp);
388 /* FIXME: we can save a few bytes here by committing if the
389 short branch is possible */
391 ppc_emit32 (code, short_branch);
393 ppc_load (code, ppc_r0, tramp);
394 ppc_mtctr (code, ppc_r0);
395 ppc_bcctr (code, 20, 0);
402 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
404 #ifdef MONO_ARCH_VTABLE_REG
407 guint8 **rgctx_null_jumps;
413 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
414 index = MONO_RGCTX_SLOT_INDEX (slot);
416 index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
417 for (depth = 0; ; ++depth) {
418 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
420 if (index < size - 1)
425 tramp_size = MONO_PPC_32_64_CASE (40, 52) + 12 * depth;
431 code = buf = mono_global_codeman_reserve (tramp_size);
433 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
437 ppc_mr (code, ppc_r4, PPC_FIRST_ARG_REG);
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);
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);
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);
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);
473 for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
474 ppc_patch (rgctx_null_jumps [i], code);
476 g_free (rgctx_null_jumps);
478 /* move the rgctx pointer to the VTABLE register */
479 ppc_mr (code, MONO_ARCH_VTABLE_REG, ppc_r3);
481 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
482 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
484 /* jump to the actual trampoline */
485 code = emit_trampoline_jump (code, tramp);
487 mono_arch_flush_icache (buf, code - buf);
489 g_assert (code - buf <= tramp_size);
493 g_assert_not_reached ();
498 mono_arch_create_generic_class_init_trampoline (void)
502 static int byte_offset = -1;
503 static guint8 bitmask;
509 code = buf = mono_global_codeman_reserve (tramp_size);
512 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
514 ppc_lbz (code, ppc_r4, byte_offset, PPC_FIRST_ARG_REG);
515 ppc_andid (code, ppc_r4, ppc_r4, bitmask);
517 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
521 ppc_patch (jump, code);
523 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
524 mono_get_root_domain (), NULL);
526 /* jump to the actual trampoline */
527 code = emit_trampoline_jump (code, tramp);
529 mono_arch_flush_icache (buf, code - buf);
531 g_assert (code - buf <= tramp_size);