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. */
16 /* Licensed under the MIT license. See LICENSE file in the project root for full license information.*/
17 /*------------------------------------------------------------------*/
19 /*------------------------------------------------------------------*/
21 /*------------------------------------------------------------------*/
23 #define LMFReg s390_r13
26 * Method-specific trampoline code fragment sizes
28 #define SPECIFIC_TRAMPOLINE_SIZE 96
30 /*========================= End of Defines =========================*/
32 /*------------------------------------------------------------------*/
34 /*------------------------------------------------------------------*/
40 #include <mono/metadata/abi-details.h>
41 #include <mono/metadata/appdomain.h>
42 #include <mono/metadata/gc-internals.h>
43 #include <mono/metadata/marshal.h>
44 #include <mono/metadata/profiler-private.h>
45 #include <mono/metadata/tabledefs.h>
46 #include <mono/arch/s390x/s390x-codegen.h>
49 #include "mini-s390x.h"
50 #include "support-s390x.h"
51 #include "jit-icalls.h"
53 /*========================= End of Includes ========================*/
55 /*------------------------------------------------------------------*/
57 /*------------------------------------------------------------------*/
60 guint8 stk[S390_MINIMAL_STACK_SIZE]; /* Standard s390x stack */
61 guint64 saveFn; /* Call address */
62 struct MonoLMF LMF; /* LMF */
65 /*========================= End of Typedefs ========================*/
67 /*------------------------------------------------------------------*/
68 /* P r o t o t y p e s */
69 /*------------------------------------------------------------------*/
71 /*========================= End of Prototypes ======================*/
73 /*------------------------------------------------------------------*/
74 /* G l o b a l V a r i a b l e s */
75 /*------------------------------------------------------------------*/
78 /*====================== End of Global Variables ===================*/
80 /*------------------------------------------------------------------*/
82 /* Name - mono_arch_get_unbox_trampoline */
84 /* Function - Return a pointer to a trampoline which does the */
85 /* unboxing before calling the method. */
87 /* When value type methods are called through the */
88 /* vtable we need to unbox the 'this' argument. */
90 /* Parameters - method - Methd pointer */
91 /* addr - Pointer to native code for method */
93 /*------------------------------------------------------------------*/
96 mono_arch_get_unbox_trampoline (MonoMethod *method, gpointer addr)
99 int this_pos = s390_r2;
100 MonoDomain *domain = mono_domain_get ();
103 start = code = mono_domain_code_reserve (domain, 28);
105 S390_SET (code, s390_r1, addr);
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);
112 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE, method);
114 snprintf(trampName, sizeof(trampName), "%s_unbox_trampoline", method->name);
116 mono_tramp_info_register (mono_tramp_info_create (trampName, start, code - start, NULL, NULL), domain);
121 /*========================= End of Function ========================*/
123 /*------------------------------------------------------------------*/
125 /* Name - mono_arch_patch_callsite */
127 /* Function - Patch a non-virtual callsite so it calls @addr. */
129 /*------------------------------------------------------------------*/
132 mono_arch_patch_callsite (guint8 *method_start, guint8 *orig_code, guint8 *addr)
135 unsigned short opcode;
137 opcode = *((unsigned short *) (orig_code - 2));
138 if (opcode == 0x0dee) {
139 /* This should be a 'iihf/iilf' sequence */
140 S390_EMIT_CALL((orig_code - 14), addr);
141 mono_arch_flush_icache (orig_code - 14, 12);
143 /* This is the 'brasl' instruction */
145 displace = ((gssize) addr - (gssize) (orig_code - 2)) / 2;
146 s390_patch_rel (orig_code, displace);
147 mono_arch_flush_icache (orig_code, 4);
151 /*========================= End of Function ========================*/
153 /*------------------------------------------------------------------*/
155 /* Name - mono_arch_patch_plt_entry. */
157 /* Function - Patch a PLT entry - unused as yet. */
159 /*------------------------------------------------------------------*/
162 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
164 g_assert_not_reached ();
167 /*========================= End of Function ========================*/
169 /*------------------------------------------------------------------*/
171 /* Name - mono_arch_create_trampoline_code */
173 /* Function - Create the designated type of trampoline according*/
174 /* to the 'tramp_type' parameter. */
176 /*------------------------------------------------------------------*/
179 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
182 guint8 *buf, *tramp, *code;
183 int i, offset, has_caller;
184 GSList *unwind_ops = NULL;
185 MonoJumpInfo *ji = NULL;
189 /* Now we'll create in 'buf' the S/390 trampoline code. This
190 is the trampoline code common to all methods */
192 code = buf = mono_global_codeman_reserve(512);
194 if ((tramp_type == MONO_TRAMPOLINE_JUMP) ||
195 (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD))
200 /*-----------------------------------------------------------
201 STEP 0: First create a non-standard function prologue with a
202 stack size big enough to save our registers.
203 -----------------------------------------------------------*/
205 s390_stmg (buf, s390_r6, s390_r15, STK_BASE, S390_REG_SAVE_OFFSET);
206 s390_lgr (buf, s390_r11, s390_r15);
207 s390_aghi (buf, STK_BASE, -sizeof(trampStack_t));
208 s390_stg (buf, s390_r11, 0, STK_BASE, 0);
210 /*---------------------------------------------------------------*/
211 /* we build the MonoLMF structure on the stack - see mini-s390.h */
212 /* Keep in sync with the code in mono_arch_emit_prolog */
213 /*---------------------------------------------------------------*/
215 s390_lgr (buf, LMFReg, STK_BASE);
216 s390_aghi (buf, LMFReg, G_STRUCT_OFFSET(trampStack_t, LMF));
218 /*---------------------------------------------------------------*/
219 /* Save general and floating point registers in LMF */
220 /*---------------------------------------------------------------*/
221 s390_stmg (buf, s390_r0, s390_r1, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
222 s390_stmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
223 s390_mvc (buf, 10*sizeof(gulong), LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[6]),
224 s390_r11, S390_REG_SAVE_OFFSET);
226 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
227 for (i = s390_f0; i <= s390_f15; ++i) {
228 s390_std (buf, i, 0, LMFReg, offset);
229 offset += sizeof(gdouble);
232 /*----------------------------------------------------------
233 STEP 1: call 'mono_get_lmf_addr()' to get the address of our
234 LMF. We'll need to restore it after the call to
235 's390_magic_trampoline' and before the call to the native
237 ----------------------------------------------------------*/
239 S390_SET (buf, s390_r1, mono_get_lmf_addr);
240 s390_basr (buf, s390_r14, s390_r1);
242 /*---------------------------------------------------------------*/
243 /* Set lmf.lmf_addr = jit_tls->lmf */
244 /*---------------------------------------------------------------*/
245 s390_stg (buf, s390_r2, 0, LMFReg,
246 G_STRUCT_OFFSET(MonoLMF, lmf_addr));
248 /*---------------------------------------------------------------*/
249 /* Get current lmf */
250 /*---------------------------------------------------------------*/
251 s390_lg (buf, s390_r0, 0, s390_r2, 0);
253 /*---------------------------------------------------------------*/
254 /* Set our lmf as the current lmf */
255 /*---------------------------------------------------------------*/
256 s390_stg (buf, LMFReg, 0, s390_r2, 0);
258 /*---------------------------------------------------------------*/
259 /* Have our lmf.previous_lmf point to the last lmf */
260 /*---------------------------------------------------------------*/
261 s390_stg (buf, s390_r0, 0, LMFReg,
262 G_STRUCT_OFFSET(MonoLMF, previous_lmf));
264 /*---------------------------------------------------------------*/
265 /* save method info */
266 /*---------------------------------------------------------------*/
267 s390_lg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
268 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, method));
270 /*---------------------------------------------------------------*/
271 /* save the current SP */
272 /*---------------------------------------------------------------*/
273 s390_lg (buf, s390_r1, 0, STK_BASE, 0);
274 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, ebp));
276 /*---------------------------------------------------------------*/
277 /* save the current IP */
278 /*---------------------------------------------------------------*/
280 s390_lg (buf, s390_r1, 0, s390_r1, S390_RET_ADDR_OFFSET);
282 s390_lghi (buf, s390_r1, 0);
284 s390_stg (buf, s390_r1, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, eip));
286 /*---------------------------------------------------------------*/
287 /* STEP 2: call the C trampoline function */
288 /*---------------------------------------------------------------*/
292 /* Arg 1: mgreg_t *regs */
293 s390_la (buf, s390_r2, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[0]));
295 /* Arg 2: code (next address to the instruction that called us) */
297 s390_lg (buf, s390_r3, 0, s390_r11, S390_RET_ADDR_OFFSET);
299 s390_lghi (buf, s390_r3, 0);
302 /* Arg 3: Trampoline argument */
303 s390_lg (buf, s390_r4, 0, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[1]));
305 /* Arg 4: trampoline address. */
306 S390_SET (buf, s390_r5, buf);
308 /* Calculate call address and call the C trampoline. Return value will be in r2 */
309 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
310 S390_SET (buf, s390_r1, tramp);
311 s390_basr (buf, s390_r14, s390_r1);
313 /* OK, code address is now on r2. Save it, so that we
314 can restore r2 and use it later */
315 s390_stg (buf, s390_r2, 0, STK_BASE, G_STRUCT_OFFSET(trampStack_t, saveFn));
317 /* Check for thread interruption */
318 S390_SET (buf, s390_r1, (guint8 *)mono_interruption_checkpoint_from_trampoline);
319 s390_basr (buf, s390_r14, s390_r1);
321 /*----------------------------------------------------------
322 STEP 3: Restore the LMF
323 ----------------------------------------------------------*/
324 restoreLMF(buf, STK_BASE, sizeof(trampStack_t));
327 s390_lg (buf, s390_r1, 0, STK_BASE, G_STRUCT_OFFSET(trampStack_t, saveFn));
329 /*----------------------------------------------------------
330 STEP 4: call the compiled method
331 ----------------------------------------------------------*/
333 /* Restore parameter registers */
334 s390_lmg (buf, s390_r2, s390_r5, LMFReg, G_STRUCT_OFFSET(MonoLMF, gregs[2]));
336 /* Restore the FP registers */
337 offset = G_STRUCT_OFFSET(MonoLMF, fregs[0]);
338 for (i = s390_f0; i <= s390_f15; ++i) {
339 s390_ld (buf, i, 0, LMFReg, offset);
340 offset += sizeof(gdouble);
343 /* Restore stack pointer and jump to the code -
344 * R14 contains the return address to our caller
346 s390_lgr (buf, STK_BASE, s390_r11);
347 s390_lmg (buf, s390_r6, s390_r14, STK_BASE, S390_REG_SAVE_OFFSET);
349 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN(tramp_type)) {
350 s390_lgr (buf, s390_r2, s390_r1);
351 s390_br (buf, s390_r14);
353 s390_br (buf, s390_r1);
356 /* Flush instruction cache, since we've generated code */
357 mono_arch_flush_icache (code, buf - code);
358 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
361 tramp_name = mono_get_generic_trampoline_name (tramp_type);
362 *info = mono_tramp_info_create (tramp_name, buf, buf - code, ji, unwind_ops);
366 g_assert ((buf - code) <= 512);
371 /*========================= End of Function ========================*/
373 /*------------------------------------------------------------------*/
375 /* Name - mono_arch_invalidate_method */
379 /*------------------------------------------------------------------*/
382 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
384 /* FIXME: This is not thread safe */
385 guint8 *code = ji->code_start;
387 S390_SET (code, s390_r1, func);
388 S390_SET (code, s390_r2, func_arg);
389 s390_br (code, s390_r1);
393 /*========================= End of Function ========================*/
395 /*------------------------------------------------------------------*/
397 /* Name - mono_arch_create_specific_trampoline */
399 /* Function - Creates the given kind of specific trampoline */
401 /*------------------------------------------------------------------*/
404 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
406 guint8 *code, *buf, *tramp;
409 tramp = mono_get_trampoline_code (tramp_type);
411 /*----------------------------------------------------------*/
412 /* This is the method-specific part of the trampoline. Its */
413 /* purpose is to provide the generic part with the */
414 /* MonoMethod *method pointer. We'll use r1 to keep it. */
415 /*----------------------------------------------------------*/
416 code = buf = mono_domain_code_reserve (domain, SPECIFIC_TRAMPOLINE_SIZE);
418 S390_SET (buf, s390_r1, arg1);
419 displace = (tramp - buf) / 2;
420 s390_jg (buf, displace);
422 /* Flush instruction cache, since we've generated code */
423 mono_arch_flush_icache (code, buf - code);
424 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE,
425 (void *) mono_get_generic_trampoline_simple_name (tramp_type));
428 g_assert ((buf - code) <= SPECIFIC_TRAMPOLINE_SIZE);
431 *code_len = buf - code;
436 /*========================= End of Function ========================*/
438 /*------------------------------------------------------------------*/
440 /* Name - mono_arch_create_rgctx_lazy_fetch_trampoline */
444 /*------------------------------------------------------------------*/
447 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
451 guint8 **rgctx_null_jumps;
459 MonoJumpInfo *ji = NULL;
460 GSList *unwind_ops = NULL;
462 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
463 index = MONO_RGCTX_SLOT_INDEX (slot);
465 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
466 for (depth = 0; ; ++depth) {
467 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
469 if (index < size - 1)
474 tramp_size = 48 + 16 * depth;
480 code = buf = mono_global_codeman_reserve (tramp_size);
482 unwind_ops = mono_arch_get_cie_program ();
484 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
488 s390_lgr (code, s390_r1, s390_r2);
490 /* load rgctx ptr from vtable */
491 s390_lg (code, s390_r1, 0, s390_r2, MONO_STRUCT_OFFSET(MonoVTable, runtime_generic_context));
492 /* is the rgctx ptr null? */
493 s390_ltgr (code, s390_r1, s390_r1);
494 /* if yes, jump to actual trampoline */
495 rgctx_null_jumps [iPatch++] = code;
499 for (i = 0; i < depth; ++i) {
500 /* load ptr to next array */
501 if (mrgctx && i == 0)
502 s390_lg (code, s390_r1, 0, s390_r1, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT);
504 s390_lg (code, s390_r1, 0, s390_r1, 0);
505 s390_ltgr (code, s390_r1, s390_r1);
506 /* if the ptr is null then jump to actual trampoline */
507 rgctx_null_jumps [iPatch++] = code;
512 s390_lg (code, s390_r1, 0, s390_r1, (sizeof (gpointer) * (index + 1)));
513 /* is the slot null? */
514 s390_ltgr (code, s390_r1, s390_r1);
515 /* if yes, jump to actual trampoline */
516 rgctx_null_jumps [iPatch++] = code;
518 /* otherwise return r1 */
519 s390_lgr (code, s390_r2, s390_r1);
520 s390_br (code, s390_r14);
522 for (i = 0; i < iPatch; i++) {
523 displace = ((uintptr_t) code - (uintptr_t) rgctx_null_jumps[i]) / 2;
524 s390_patch_rel ((rgctx_null_jumps [i] + 2), displace);
527 g_free (rgctx_null_jumps);
529 /* move the rgctx pointer to the VTABLE register */
530 s390_lgr (code, MONO_ARCH_VTABLE_REG, s390_r2);
532 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
533 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
535 /* jump to the actual trampoline */
536 displace = (tramp - code) / 2;
537 s390_jg (code, displace);
539 mono_arch_flush_icache (buf, code - buf);
540 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE, NULL);
542 g_assert (code - buf <= tramp_size);
544 char *name = mono_get_rgctx_fetch_trampoline_name (slot);
545 *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
551 /*========================= End of Function ========================*/
553 /*------------------------------------------------------------------*/
555 /* Name - mono_arch_get_static_rgctx_trampoline */
557 /* Function - Create a trampoline which sets RGCTX_REG to MRGCTX*/
558 /* then jumps to ADDR. */
560 /*------------------------------------------------------------------*/
563 mono_arch_get_static_rgctx_trampoline (MonoMethod *m,
564 MonoMethodRuntimeGenericContext *mrgctx,
567 guint8 *code, *start;
572 MonoDomain *domain = mono_domain_get ();
576 start = code = mono_domain_code_reserve (domain, buf_len);
578 S390_SET (code, MONO_ARCH_RGCTX_REG, mrgctx);
579 displace = ((uintptr_t) addr - (uintptr_t) code) / 2;
580 s390_jg (code, displace);
581 g_assert ((code - start) < buf_len);
583 mono_arch_flush_icache (start, code - start);
584 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
586 snprintf(trampName, sizeof(trampName), "%s_rgctx_trampoline", m->name);
588 mono_tramp_info_register (mono_tramp_info_create (trampName, start, code - start, NULL, NULL), domain);
593 /*========================= End of Function ========================*/
595 /*------------------------------------------------------------------*/
597 /* Name - handler_block_trampoline_helper */
601 /*------------------------------------------------------------------*/
604 handler_block_trampoline_helper (gpointer *ptr)
606 MonoJitTlsData *jit_tls = mono_tls_get_jit_tls ();
607 *ptr = jit_tls->handler_block_return_address;
610 /*========================= End of Function ========================*/
612 /*------------------------------------------------------------------*/
614 /* Name - mono_arch_create_handler_block_trampoline */
618 /*------------------------------------------------------------------*/
621 mono_arch_create_handler_block_trampoline (MonoTrampInfo **info, gboolean aot)
623 guint8 *tramp = mono_get_trampoline_code (MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD);
626 MonoJumpInfo *ji = NULL;
627 GSList *unwind_ops = NULL;
631 code = buf = mono_global_codeman_reserve (tramp_size);
634 * This trampoline restore the call chain of the handler block
635 * then jumps into the code that deals with it.
639 * Slow path uses a C helper
641 S390_SET (code, s390_r2, tramp);
642 S390_SET (code, s390_r1, handler_block_trampoline_helper);
643 s390_br (code, s390_r1);
645 mono_arch_flush_icache (buf, code - buf);
646 mono_profiler_code_buffer_new (buf, code - buf, MONO_PROFILER_CODE_BUFFER_HELPER, NULL);
647 g_assert (code - buf <= tramp_size);
649 *info = mono_tramp_info_create ("handler_block_trampoline", buf, code - buf, ji, unwind_ops);
654 /*========================= End of Function ========================*/