1 /*------------------------------------------------------------------*/
3 /* Name - tramp-s390x.c */
5 /* Function - JIT trampoline code for S/390. */
7 /* Name - Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com) */
9 /* Date - January, 2004 */
11 /* Derivation - From exceptions-x86 & exceptions-ppc */
12 /* Paolo Molaro (lupus@ximian.com) */
13 /* Dietmar Maurer (dietmar@ximian.com) */
15 /* Copyright - 2001 Ximian, Inc. */
17 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
21 /*------------------------------------------------------------------*/
23 #define GR_SAVE_SIZE 4*sizeof(long)
24 #define FP_SAVE_SIZE 16*sizeof(double)
25 #define METHOD_SAVE_OFFSET S390_MINIMAL_STACK_SIZE
26 #define CREATE_GR_OFFSET METHOD_SAVE_OFFSET+8
27 #define CREATE_FP_OFFSET CREATE_GR_OFFSET+GR_SAVE_SIZE
28 #define CREATE_LMF_OFFSET CREATE_FP_OFFSET+FP_SAVE_SIZE
29 #define CREATE_STACK_SIZE (CREATE_LMF_OFFSET+2*sizeof(long)+sizeof(MonoLMF))
30 #define GENERIC_REG_OFFSET CREATE_STACK_SIZE + \
31 S390_REG_SAVE_OFFSET + \
34 /*------------------------------------------------------------------*/
35 /* Method-specific trampoline code fragment sizes */
36 /*------------------------------------------------------------------*/
37 #define SPECIFIC_TRAMPOLINE_SIZE 96
39 /*========================= End of Defines =========================*/
41 /*------------------------------------------------------------------*/
43 /*------------------------------------------------------------------*/
49 #include <mono/metadata/appdomain.h>
50 #include <mono/metadata/marshal.h>
51 #include <mono/metadata/tabledefs.h>
52 #include <mono/arch/s390x/s390x-codegen.h>
55 #include "mini-s390x.h"
57 /*========================= End of Includes ========================*/
59 /*------------------------------------------------------------------*/
61 /*------------------------------------------------------------------*/
63 /*========================= End of Typedefs ========================*/
65 /*------------------------------------------------------------------*/
66 /* P r o t o t y p e s */
67 /*------------------------------------------------------------------*/
69 /*========================= End of Prototypes ======================*/
71 /*------------------------------------------------------------------*/
72 /* G l o b a l V a r i a b l e s */
73 /*------------------------------------------------------------------*/
76 /*====================== End of Global Variables ===================*/
78 /*------------------------------------------------------------------*/
80 /* Name - mono_arch_get_unbox_trampoline */
82 /* Function - Return a pointer to a trampoline which does the */
83 /* unboxing before calling the method. */
85 /* When value type methods are called through the */
86 /* vtable we need to unbox the 'this' argument. */
88 /* Parameters - method - Methd pointer */
89 /* addr - Pointer to native code for method */
91 /*------------------------------------------------------------------*/
94 mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
97 int this_pos = s390_r2;
98 MonoDomain *domain = mono_domain_get ();
100 start = code = mono_domain_code_reserve (domain, 28);
102 s390_basr (code, s390_r1, 0);
104 s390_llong(code, addr);
105 s390_lg (code, s390_r1, 0, s390_r1, 4);
106 s390_aghi (code, this_pos, sizeof(MonoObject));
107 s390_br (code, s390_r1);
109 g_assert ((code - start) <= 28);
111 mono_arch_flush_icache (start, code - start);
116 /*========================= End of Function ========================*/
118 /*------------------------------------------------------------------*/
120 /* Name - mono_arch_patch_callsite */
122 /* Function - Patch a non-virtual callsite so it calls @addr. */
124 /*------------------------------------------------------------------*/
127 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
130 unsigned short opcode;
132 opcode = *((unsigned short *) (orig_code - 6));
133 if (opcode == 0xc0e5) {
134 /* This is the 'brasl' instruction */
136 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
137 s390_patch_rel (orig_code, displace);
138 mono_arch_flush_icache (orig_code, 4);
140 /* This should be a 'lg %r14,4(%r13)' then a 'basr r14, r14' instruction */
141 g_assert (orig_code [-8] == 0xe3);
142 g_assert (orig_code [-7] == 0xe0);
143 g_assert (orig_code [-6] == 0xd0);
144 g_assert (orig_code [-5] == 0x04);
145 g_assert (orig_code [-4] == 0x00);
146 g_assert (orig_code [-3] == 0x04);
147 opcode = *((unsigned short*) (orig_code - 2));
148 g_assert (opcode == 0x0dee);
150 /* The call address is stored in the 8 bytes preceeding the basr instruction */
151 s390_patch_addr(orig_code - 16, (gssize)addr);
152 mono_arch_flush_icache (orig_code - 16, 8);
156 /*========================= End of Function ========================*/
158 /*------------------------------------------------------------------*/
160 /* Name - mono_arch_patch_plt_entry. */
162 /* Function - Patch a PLT entry - unused as yet. */
164 /*------------------------------------------------------------------*/
167 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
169 g_assert_not_reached ();
172 /*========================= End of Function ========================*/
174 /*------------------------------------------------------------------*/
176 /* Name - mono_arch_nullify_class_init_trampoline */
178 /* Function - Nullify a call which calls a class init trampoline*/
180 /*------------------------------------------------------------------*/
183 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
185 char patch[2] = {0x07, 0x00};
189 memcpy(code, patch, sizeof(patch));
192 /*========================= End of Function ========================*/
194 /*------------------------------------------------------------------*/
196 /* Name - mono_arch_get_nullified_class_init */
198 /* Function - Nullify a PLT entry call. */
200 /*------------------------------------------------------------------*/
203 mono_arch_get_nullified_class_init_trampoline (MonoTrampInfo **info)
207 code = buf = mono_global_codeman_reserve (16);
209 s390_br (code, s390_r14);
212 *info = mono_tramp_info_create ("nullified_class_init_trampoline",
213 buf, code - buf, NULL, NULL);
218 /*========================= End of Function ========================*/
220 /*------------------------------------------------------------------*/
222 /* Name - mono_arch_create_trampoline_code */
224 /* Function - Create the designated type of trampoline according*/
225 /* to the 'tramp_type' parameter. */
227 /*------------------------------------------------------------------*/
230 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
233 guint8 *buf, *tramp, *code;
234 int i, offset, lmfOffset;
235 GSList *unwind_ops = NULL;
236 MonoJumpInfo *ji = NULL;
240 /* Now we'll create in 'buf' the S/390 trampoline code. This
241 is the trampoline code common to all methods */
243 code = buf = mono_global_codeman_reserve(512);
245 /*-----------------------------------------------------------
246 STEP 0: First create a non-standard function prologue with a
247 stack size big enough to save our registers.
248 -----------------------------------------------------------*/
250 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
251 s390_lgr (buf, s390_r11, s390_r15);
252 s390_aghi (buf, STK_BASE, -CREATE_STACK_SIZE);
253 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
254 s390_stg (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
255 s390_stmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
257 /* Save the FP registers */
258 offset = CREATE_FP_OFFSET;
259 for (i = s390_f0; i <= s390_f15; ++i) {
260 s390_std (buf, i, 0, STK_BASE, offset);
264 /*----------------------------------------------------------
265 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
266 LMF. We'll need to restore it after the call to
267 's390_magic_trampoline' and before the call to the native
269 ----------------------------------------------------------*/
271 s390_basr (buf, s390_r13, 0);
273 s390_llong(buf, mono_get_lmf_addr);
274 s390_lg (buf, s390_r1, 0, s390_r13, 4);
275 s390_basr (buf, s390_r14, s390_r1);
277 /*---------------------------------------------------------------*/
278 /* we build the MonoLMF structure on the stack - see mini-s390.h */
279 /* Keep in sync with the code in mono_arch_emit_prolog */
280 /*---------------------------------------------------------------*/
281 lmfOffset = CREATE_STACK_SIZE - sizeof(MonoLMF);
283 s390_lgr (buf, s390_r13, STK_BASE);
284 s390_aghi (buf, s390_r13, lmfOffset);
286 /*---------------------------------------------------------------*/
287 /* Set lmf.lmf_addr = jit_tls->lmf */
288 /*---------------------------------------------------------------*/
289 s390_stg (buf, s390_r2, 0, s390_r13,
290 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
292 /*---------------------------------------------------------------*/
293 /* Get current lmf */
294 /*---------------------------------------------------------------*/
295 s390_lg (buf, s390_r0, 0, s390_r2, 0);
297 /*---------------------------------------------------------------*/
298 /* Set our lmf as the current lmf */
299 /*---------------------------------------------------------------*/
300 s390_stg (buf, s390_r13, 0, s390_r2, 0);
302 /*---------------------------------------------------------------*/
303 /* Have our lmf.previous_lmf point to the last lmf */
304 /*---------------------------------------------------------------*/
305 s390_stg (buf, s390_r0, 0, s390_r13,
306 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
308 /*---------------------------------------------------------------*/
309 /* save method info */
310 /*---------------------------------------------------------------*/
311 s390_lg (buf, s390_r1, 0, STK_BASE, METHOD_SAVE_OFFSET);
312 s390_stg (buf, s390_r1, 0, s390_r13,
313 G_STRUCT_OFFSET(MonoLMF, method));
315 /*---------------------------------------------------------------*/
316 /* save the current SP */
317 /*---------------------------------------------------------------*/
318 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
319 s390_stg (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, ebp));
321 /*---------------------------------------------------------------*/
322 /* save the current IP */
323 /*---------------------------------------------------------------*/
324 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
325 s390_lghi (buf, s390_r1, 0);
327 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
328 // s390_la (buf, s390_r1, 0, s390_r1, 0);
330 s390_stg (buf, s390_r1, 0, s390_r13, G_STRUCT_OFFSET(MonoLMF, eip));
332 /*---------------------------------------------------------------*/
333 /* Save general and floating point registers */
334 /*---------------------------------------------------------------*/
335 s390_mvc (buf, 4*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[2]),
336 STK_BASE, CREATE_GR_OFFSET);
337 s390_mvc (buf, 10*sizeof(gulong), s390_r13, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
338 s390_r11, S390_REG_SAVE_OFFSET);
340 /* Simply copy fpregs already saved above */
341 s390_mvc (buf, 16*sizeof(double), s390_r13, G_STRUCT_OFFSET(MonoLMF, fregs[0]),
342 STK_BASE, CREATE_FP_OFFSET);
344 /*---------------------------------------------------------------*/
345 /* STEP 2: call the C trampoline function */
346 /*---------------------------------------------------------------*/
350 /* Arg 1: mgreg_t *regs. We pass sp instead */
351 s390_la (buf, s390_r2, 0, STK_BASE, CREATE_STACK_SIZE);
353 /* Arg 2: code (next address to the instruction that called us) */
354 if (tramp_type == MONO_TRAMPOLINE_JUMP) {
355 s390_lghi (buf, s390_r3, 0);
357 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
360 /* Arg 3: Trampoline argument */
361 if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
362 s390_lg (buf, s390_r4, 0, STK_BASE, GENERIC_REG_OFFSET);
364 s390_lg (buf, s390_r4, 0, STK_BASE, METHOD_SAVE_OFFSET);
366 /* Arg 4: trampoline address. Ignore for now */
368 /* Calculate call address and call the C trampoline. Return value will be in r2 */
369 s390_basr (buf, s390_r13, 0);
371 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
372 s390_llong (buf, tramp);
373 s390_lg (buf, s390_r1, 0, s390_r13, 4);
374 s390_basr (buf, s390_r14, s390_r1);
376 /* OK, code address is now on r2. Move it to r1, so that we
377 can restore r2 and use it from r1 later */
378 s390_lgr (buf, s390_r1, s390_r2);
380 /*----------------------------------------------------------
381 STEP 3: Restore the LMF
382 ----------------------------------------------------------*/
383 restoreLMF(buf, STK_BASE, CREATE_STACK_SIZE);
385 /*----------------------------------------------------------
386 STEP 4: call the compiled method
387 ----------------------------------------------------------*/
389 /* Restore registers */
391 s390_lmg (buf, s390_r2, s390_r5, STK_BASE, CREATE_GR_OFFSET);
393 /* Restore the FP registers */
394 offset = CREATE_FP_OFFSET;
395 for (i = s390_f0; i <= s390_f15; ++i) {
396 s390_ld (buf, i, 0, STK_BASE, offset);
400 /* Restore stack pointer and jump to the code -
401 R14 contains the return address to our caller */
402 s390_lgr (buf, STK_BASE, s390_r11);
403 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
405 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
406 s390_lgr (buf, s390_r2, s390_r1);
407 s390_br (buf, s390_r14);
409 s390_br (buf, s390_r1);
412 /* Flush instruction cache, since we've generated code */
413 mono_arch_flush_icache (code, buf - code);
416 tramp_name = mono_get_generic_trampoline_name (tramp_type);
417 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
422 g_assert ((buf - code) <= 512);
427 /*========================= End of Function ========================*/
429 /*------------------------------------------------------------------*/
431 /* Name - mono_arch_create_specific_trampoline */
433 /* Function - Creates the given kind of specific trampoline */
435 /*------------------------------------------------------------------*/
438 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
440 guint8 *code, *buf, *tramp;
443 tramp = mono_get_trampoline_code (tramp_type);
445 /*----------------------------------------------------------*/
446 /* This is the method-specific part of the trampoline. Its */
447 /* purpose is to provide the generic part with the */
448 /* MonoMethod *method pointer. We'll use r1 to keep it. */
449 /*----------------------------------------------------------*/
450 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
452 s390_basr (buf, s390_r1, 0);
454 s390_llong(buf, arg1);
455 s390_lg (buf, s390_r1, 0, s390_r1, 4);
456 displace = (tramp - buf) / 2;
457 s390_jg (buf, displace);
459 /* Flush instruction cache, since we've generated code */
460 mono_arch_flush_icache (code, buf - code);
463 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
466 *code_len = buf - code;
471 /*========================= End of Function ========================*/
473 /*------------------------------------------------------------------*/
475 /* Name - mono_arch_create_rgctx_lazy_fetch_trampoline */
479 /*------------------------------------------------------------------*/
482 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
484 #ifdef MONO_ARCH_VTABLE_REG
487 guint8 **rgctx_null_jumps;
495 MonoJumpInfo *ji = NULL;
496 GSList *unwind_ops = NULL;
498 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
499 index = MONO_RGCTX_SLOT_INDEX (slot);
501 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
502 for (depth = 0; ; ++depth) {
503 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
505 if (index < size - 1)
510 tramp_size = 48 + 16 * depth;
516 code = buf = mono_global_codeman_reserve (tramp_size);
518 unwind_ops = mono_arch_get_cie_program ();
520 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
524 s390_lgr (code, s390_r1, s390_r2);
526 /* load rgctx ptr from vtable */
527 s390_lg (code, s390_r1, 0, s390_r2, G_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
528 /* is the rgctx ptr null? */
529 s390_ltgr (code, s390_r1, s390_r1);
530 /* if yes, jump to actual trampoline */
531 rgctx_null_jumps [iPatch++] = code;
535 for (i = 0; i < depth; ++i) {
536 /* load ptr to next array */
537 if (mrgctx && i == 0)
538 s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
540 s390_lg (code, s390_r1, 0, s390_r1, 0);
541 s390_ltgr (code, s390_r1, s390_r1);
542 /* if the ptr is null then jump to actual trampoline */
543 rgctx_null_jumps [iPatch++] = code;
548 s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index + 1)));
549 /* is the slot null? */
550 s390_ltgr (code, s390_r1, s390_r1);
551 /* if yes, jump to actual trampoline */
552 rgctx_null_jumps [iPatch++] = code;
554 /* otherwise return r1 */
555 s390_lgr (code, s390_r2, s390_r1);
556 s390_br (code, s390_r14);
558 for (i = 0; i < iPatch; i++) {
559 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
560 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
563 g_free (rgctx_null_jumps);
565 /* move the rgctx pointer to the VTABLE register */
566 s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
568 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
569 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
571 /* jump to the actual trampoline */
572 displace = (tramp - code) / 2;
573 s390_jg (code, displace);
575 mono_arch_flush_icache (buf, code - buf);
577 g_assert (code - buf <= tramp_size);
580 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
581 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
587 g_assert_not_reached ();
592 /*========================= End of Function ========================*/
594 /*------------------------------------------------------------------*/
596 /* Name - mono_arch_get_static_rgctx_trampoline */
598 /* Function - Create a trampoline which sets RGCTX_REG to MRGCTX*/
599 /* then jumps to ADDR. */
601 /*------------------------------------------------------------------*/
604 mono_arch_get_static_rgctx_trampoline (MonoMethod *m,
605 MonoMethodRuntimeGenericContext *mrgctx,
608 guint8 *code, *start;
612 MonoDomain *domain = mono_domain_get ();
616 start = code = mono_domain_code_reserve (domain, buf_len);
618 s390_basr (code, s390_r1, 0);
620 s390_llong(code, mrgctx);
621 s390_lg (code, MONO_ARCH_RGCTX_REG, 0, s390_r1, 4);
622 displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
623 s390_jg (code, displace);
624 g_assert ((code - start) < buf_len);
626 mono_arch_flush_icache (start, code - start);
631 /*========================= End of Function ========================*/
633 /*------------------------------------------------------------------*/
635 /* Name - mono_arch_create_generic_class_init_trampoline */
639 /*------------------------------------------------------------------*/
642 mono_arch_create_generic_class_init_trampoline (MonoTrampInfo **info, gboolean aot)
646 static int byte_offset = -1;
647 static guint8 bitmask;
651 GSList *unwind_ops = NULL;
652 MonoJumpInfo *ji = NULL;
656 code = buf = mono_global_codeman_reserve (tramp_size);
658 unwind_ops = mono_arch_get_cie_program ();
661 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
663 s390_llgc(code, s390_r0, 0, MONO_ARCH_VTABLE_REG, byte_offset);
664 s390_nill(code, s390_r0, bitmask);
665 s390_bnzr(code, s390_r14);
667 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
668 mono_get_root_domain (), NULL);
670 /* jump to the actual trampoline */
671 displace = (tramp - code) / 2;
672 s390_jg (code, displace);
674 mono_arch_flush_icache (buf, code - buf);
676 g_assert (code - buf <= tramp_size);
679 *info = mono_tramp_info_create ("generic_class_init_trampoline", buf, code - buf, ji, unwind_ops);
684 /*========================= End of Function ========================*/